本文主要是介绍【Unity】动作游戏开发实战详细分析-17-滑轨相机,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
【Unity】动作游戏开发实战详细分析-17-滑轨相机
构建贝塞尔曲线
使用插件可以帮助构建贝塞尔曲线,我们通过它来构建相机滑轨路径,我们这里主要使用UnityExtensions开源工具包
滑轨相机代码思路
我们需要两条曲线,一条是玩家可能会走的移动映射曲线,一条是相机的滑轨曲线。
我们通过计算玩家在映射曲线上的位置,通过比例计算来映射到相机滑轨曲线上,计算相机的滑轨移动。
映射逻辑如下图
对于不同的曲线构建插件,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;//最终方向信息}
}
这篇关于【Unity】动作游戏开发实战详细分析-17-滑轨相机的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!