这段代码定义了一个名为ThirdPersonFollowDistanceModifier的类,该类继承自MonoBehaviour,用于在Unity引擎中修改带有Cinemachine3rdPersonFollow组件的虚拟摄像机的视角距离。下面是对代码的逐步分解与详细解释:
- 命名空间与类定义:
- namespace Cinemachine.Examples
- {
- public class ThirdPersonFollowDistanceModifier : MonoBehaviour
CopyInsert
-
- 定义了一个名为Cinemachine.Examples的命名空间。
- 类ThirdPersonFollowDistanceModifier继承自MonoBehaviour,这意味着该类将作为Unity游戏对象上的组件存在。
- 类的注释:
- /// <summary>
- /// This is an add-on for Cinemachine virtual cameras containing the ThirdPersonFollow component.
- /// It modifies the camera distance as a function of vertical angle.
- /// </summary>
CopyInsert
-
- 这段注释描述了该类的作用,即作为Cinemachine虚拟摄像机的附加组件,用于根据摄像机的垂直角度调整其距离跟随的目标。
- 属性及其注释:
- [Tooltip("Camera angle that corresponds to the start of the distance graph")]
- public float MinAngle;
- [Tooltip("Camera angle that corresponds to the end of the distance graph")]
- public float MaxAngle;
- [Tooltip("Defines how the camera distance scales as a function of vertical camera angle. "
- + "X axis of graph go from 0 to 1, Y axis is the multiplier that will be "
- + "applied to the base distance.")]
- public AnimationCurve DistanceScale;
CopyInsert
-
- MinAngle和MaxAngle分别定义了摄像机垂直角度的最小值和最大值。
- DistanceScale是一个AnimationCurve对象,用于定义摄像机距离与垂直角度之间的缩放关系。它将垂直角度映射到一个0到1的范围,然后通过这个范围来计算实际的摄像机距离缩放比例。
- 私有变量:
- Cinemachine3rdPersonFollow TpsFollow;
- Transform FollowTarget;
- float BaseDistance;
CopyInsert
-
- TpsFollow是指向虚拟摄像机上的Cinemachine3rdPersonFollow组件的引用。
- FollowTarget是指向虚拟摄像机跟随的目标的Transform。
- BaseDistance存储了原始的摄像机距离,以便在调整后能够恢复到初始状态。
- Reset方法:
- void Reset()
- {
- MinAngle = -90;
- MaxAngle = 90;
- DistanceScale = AnimationCurve.EaseInOut(0, 0.5f, 1, 2);
- }
CopyInsert
-
- 在Unity编辑器中,当用户重置这个脚本组件时,MinAngle将被设置为-90度,MaxAngle将被设置为90度,DistanceScale将被设置为一个预定义的缓动曲线(由EaseInOut方法创建),该曲线在X轴从0到1变化时,Y轴从0.5增加到2。
- OnEnable方法:
- void OnEnable()
- {
- var vcam = GetComponentInChildren<CinemachineVirtualCamera>();
- if (vcam != null)
- {
- TpsFollow = vcam.GetCinemachineComponent<Cinemachine3rdPersonFollow>();
- FollowTarget = vcam.Follow;
- }
- if (TpsFollow != null)
- BaseDistance = TpsFollow.CameraDistance;
- }
CopyInsert
-
- 当组件被激活时,此方法会寻找子对象或自身上的CinemachineVirtualCamera组件,并获取其Cinemachine3rdPersonFollow组件和跟随目标。
- 如果找到了Cinemachine3rdPersonFollow组件,它将存储原始的摄像机距离于BaseDistance变量中。
- OnDisable方法:
- void OnDisable()
- {
- if (TpsFollow != null)
- TpsFollow.CameraDistance = BaseDistance;
- }
CopyInsert
-
- 当组件被禁用时,此方法将恢复摄像机的原始距离,即BaseDistance,以确保在组件启用和禁用之间摄像机的行为是一致的。
- Update方法:
- void Update()
- {
- if (TpsFollow != null && FollowTarget != null)
- {
- var xRot = FollowTarget.rotation.eulerAngles.x;
- if (xRot > 180)
- xRot -= 360;
- var t = (xRot - MinAngle) / (MaxAngle - MinAngle);
- TpsFollow.CameraDistance = BaseDistance * DistanceScale.Evaluate(t);
- }
- }
CopyInsert
-
- 在每一帧的更新中,如果TpsFollow和FollowTarget都不为null,则获取跟随目标的X轴旋转角度。
- 如果X轴旋转角度大于180度,减去360度以得到正确的旋转值。这步是为了处理旋转角度在Unity中可能超过180度的情况。
- 计算t,它是一个归一化的值,范围在0到1之间,代表X轴旋转角度在MinAngle和MaxAngle之间的相对位置。
- 使用t来评估DistanceScale曲线上的Y值,然后将这个值乘以BaseDistance以获得新的摄像机距离,并将其赋值给TpsFollow.CameraDistance。
总结:
- 该脚本的主要功能是根据跟随目标的垂直旋转角度动态调整Cinemachine虚拟摄像机的视角距离。它允许开发者通过设置MinAngle、MaxAngle以及DistanceScale曲线来定义摄像机距离如何随着角度的变化而变化。
- 通过在OnEnable中存储原始距离,并在OnDisable中恢复,从而确保即使组件被启用和禁用,摄像机的行为也不会受到影响。
- 这个功能可以用于创建更加自然和响应玩家动作的第三人称视角跟随效果。
2万+

被折叠的 条评论
为什么被折叠?



