【Unity】动作游戏开发实战详细分析-17-滑轨相机


构建贝塞尔曲线

使用插件可以帮助构建贝塞尔曲线,我们通过它来构建相机滑轨路径,我们这里主要使用UnityExtensions开源工具包

滑轨相机代码思路

我们需要两条曲线,一条是玩家可能会走的移动映射曲线,一条是相机的滑轨曲线。

我们通过计算玩家在映射曲线上的位置,通过比例计算来映射到相机滑轨曲线上,计算相机的滑轨移动。

映射逻辑如下图

Unity 计算轨道 unity轨道相机_贝塞尔曲线

对于不同的曲线构建插件,API可能会有所不同,但是思路和想法是相同的。

代码实现

滑轨相机
public class RailCamera : MonoBehaviour
{
  public Vector3 focusOffset = new Vector3(0, 1.5f, 0);//玩家点偏移
  public float moveSpeed = 30f;//相机移动速度
  public float stepLength = 0.1f;//贝塞尔曲线每分步长度
  public float tween = 17f;//相机缓动插值
  bool mCutEnter;//进入RailCamera是否直接切镜头
  bool mIsInitialized;//是否初始化完毕
  Transform mPlayerTransform;
  BezierPath mCameraPath;//相机路径
  BezierPath mMappingPath;//映射路径


  DirectionGuidePath mDirectionGuidePath;
  public DirectionGuidePath DirectionGuidePath { get { return mDirectionGuidePath; } }


  public void SetDirectionGuidePath(DirectionGuidePath directionGuidePath)
  {
    mDirectionGuidePath = directionGuidePath;
  }

  public void Setup(BezierPath cameraPath, BezierPath mappingPath, bool cutEnter)
  {
    mCutEnter = cutEnter;
    mCameraPath = cameraPath;
    mMappingPath = mappingPath;
    const string PLAYER = "Player";
    mPlayerTransform = GameObject.FindGameObjectWithTag(PLAYER).transform;
    mIsInitialized = true;
  }

  void Update()
  {
    if (!mIsInitialized) return;
    var focusPoint = mPlayerTransform.localToWorldMatrix.MultiplyPoint3x4(focusOffset);
    //玩家点
    var focusTransformLocation = mMappingPath.GetClosestLocation(focusPoint, stepLength);
    //玩家点在曲线上的位置信息
    if (mCutEnter)//如果直接切镜头到曲线相机
    {
      var mappingRate = mMappingPath.GetLength(focusTransformLocation) / mMappingPath.length;
      //映射曲线中的百分比
      var point = mCameraPath.GetPoint(mCameraPath.GetLocationByLength(mappingRate * mCameraPath.length));
      //原曲线的位置
      transform.position = point;
      mCutEnter = false;
    }
    var currentCameraLocation = mCameraPath.GetClosestLocation(transform.position, stepLength);
    //相机接近的相机曲线位置
    var currentMappingLength = (mCameraPath.GetLength(currentCameraLocation) / mCameraPath.length) * mMappingPath.length;
    //映射曲线长度
    var expectMappingLength = mMappingPath.GetLength(focusTransformLocation);//玩家当前的映射曲线长度
    var finalMappingLength = Mathf.Lerp(currentMappingLength, expectMappingLength, moveSpeed * Time.smoothDeltaTime);
    //映射曲线步进
    var currentBezierLength = (finalMappingLength / mMappingPath.length) * mCameraPath.length;
    //转换回相机曲线
    var currentBezierLocation = mCameraPath.GetLocationByLength(currentBezierLength);
    transform.position = Vector3.Lerp(transform.position, mCameraPath.GetPoint(currentBezierLocation), tween * Time.smoothDeltaTime);
    //位置赋值并缓动插值
    transform.LookAt(focusPoint);//旋转信息赋值,直接看向目标
  }
}

我们只需要通过一个滑轨触发器来触发它即可,如果还有一个第三人称相机控制,此时应该关闭它,否则多重控制会导致镜头抖动。

public class RailCameraTriggerBox : MonoBehaviour
{
  const string PLAYER_TAG = "Player";

  public RailCamera railCamera;//滑轨相机对象
  public BezierPath cameraPath;//相机路径
  public BezierPath mappingPath;//y映射路径
  public bool cutEnter;//是否直接切镜头


  private void OnTriggerEnter(Collider other)
  {
    if (!other.CompareTag(PLAYER_TAG)) return;

    railCamera.Setup(cameraPath, mappingPath, cutEnter);//启动
    gameObject.SetActive(false);//避免重复触发
  }
}

路径修正

在滑轨相机观测下,遥感前推的移动方向未必是镜头前发,此时它的移动方向将会不同。我们通过新的路径计算来修正移动方向。这里使用了多个点位的加权运算。

public class DirectionGuidePath : MonoBehaviour
{
  public BezierPath bezierPath;//目标曲线
  public Transform[] keywordPoints;//重定位方向
  public float stepLength = 0.1f;//曲线每分步长度


  public Vector3 GetGuideDirection(Vector3 point)
  {
    var result = new Vector3();
    var location = bezierPath.GetClosestLocation(point, stepLength);
    var playerClosestPoint = bezierPath.GetPoint(location);//玩家在曲线上位置
    var sumD = 0f;
    for (int i = 0; i < keywordPoints.Length; i++)
      sumD += Vector3.Distance(playerClosestPoint, keywordPoints[i].transform.position);
    var sum2 = 0f;
    for (int i = 0; i < keywordPoints.Length; i++)
    {
      var distance = Vector3.Distance(playerClosestPoint, keywordPoints[i].transform.position);
      sum2 += sumD / distance;
    }//计算多权重混合
    for (int i = 0; i < keywordPoints.Length; i++)
    {
      var keywordPointTransform = keywordPoints[i];
      var distance = Vector3.Distance(playerClosestPoint, keywordPointTransform.position);
      var pointsWeight = (sumD / distance) / sum2;//多权重混合系数
      result += keywordPointTransform.forward * pointsWeight;
    }
    return result.normalized;//最终方向信息
  }
}