unity虚拟相机控制

这段代码定义了一个名为ThirdPersonFollowDistanceModifier的类,该类继承自MonoBehaviour,用于在Unity引擎中修改带有Cinemachine3rdPersonFollow组件的虚拟摄像机的视角距离。下面是对代码的逐步分解与详细解释:

  1. 命名空间与类定义
  2. namespace Cinemachine.Examples
  3. {
  4.     public class ThirdPersonFollowDistanceModifier : MonoBehaviour

CopyInsert

    • 定义了一个名为Cinemachine.Examples的命名空间。
    • 类ThirdPersonFollowDistanceModifier继承自MonoBehaviour,这意味着该类将作为Unity游戏对象上的组件存在。
  1. 类的注释
  2. /// <summary>
  3. /// This is an add-on for Cinemachine virtual cameras containing the ThirdPersonFollow component.
  4. /// It modifies the camera distance as a function of vertical angle.
  5. /// </summary>

CopyInsert

    • 这段注释描述了该类的作用,即作为Cinemachine虚拟摄像机的附加组件,用于根据摄像机的垂直角度调整其距离跟随的目标。
  1. 属性及其注释
  2. [Tooltip("Camera angle that corresponds to the start of the distance graph")]
  3. public float MinAngle;
  4. [Tooltip("Camera angle that corresponds to the end of the distance graph")]
  5. public float MaxAngle;
  6. [Tooltip("Defines how the camera distance scales as a function of vertical camera angle.  "
  7.     + "X axis of graph go from 0 to 1, Y axis is the multiplier that will be "
  8.     + "applied to the base distance.")]
  9. public AnimationCurve DistanceScale;

CopyInsert

    • MinAngle和MaxAngle分别定义了摄像机垂直角度的最小值和最大值。
    • DistanceScale是一个AnimationCurve对象,用于定义摄像机距离与垂直角度之间的缩放关系。它将垂直角度映射到一个0到1的范围,然后通过这个范围来计算实际的摄像机距离缩放比例。
  1. 私有变量
  2. Cinemachine3rdPersonFollow TpsFollow;
  3. Transform FollowTarget;
  4. float BaseDistance;

CopyInsert

    • TpsFollow是指向虚拟摄像机上的Cinemachine3rdPersonFollow组件的引用。
    • FollowTarget是指向虚拟摄像机跟随的目标的Transform。
    • BaseDistance存储了原始的摄像机距离,以便在调整后能够恢复到初始状态。
  1. Reset方法
  2. void Reset()
  3. {
  4.     MinAngle = -90;
  5.     MaxAngle = 90;
  6.     DistanceScale = AnimationCurve.EaseInOut(0, 0.5f, 1, 2);
  7. }

CopyInsert

    • 在Unity编辑器中,当用户重置这个脚本组件时,MinAngle将被设置为-90度,MaxAngle将被设置为90度,DistanceScale将被设置为一个预定义的缓动曲线(由EaseInOut方法创建),该曲线在X轴从0到1变化时,Y轴从0.5增加到2。
  1. OnEnable方法
  2. void OnEnable()
  3. {
  4.     var vcam = GetComponentInChildren<CinemachineVirtualCamera>();
  5.     if (vcam != null)
  6.     {
  7.         TpsFollow = vcam.GetCinemachineComponent<Cinemachine3rdPersonFollow>();
  8.         FollowTarget = vcam.Follow;
  9.     }
  10.     if (TpsFollow != null)
  11.         BaseDistance = TpsFollow.CameraDistance;
  12. }

CopyInsert

    • 当组件被激活时,此方法会寻找子对象或自身上的CinemachineVirtualCamera组件,并获取其Cinemachine3rdPersonFollow组件和跟随目标。
    • 如果找到了Cinemachine3rdPersonFollow组件,它将存储原始的摄像机距离于BaseDistance变量中。
  1. OnDisable方法
  2. void OnDisable()
  3. {
  4.     if (TpsFollow != null)
  5.         TpsFollow.CameraDistance = BaseDistance;
  6. }

CopyInsert

    • 当组件被禁用时,此方法将恢复摄像机的原始距离,即BaseDistance,以确保在组件启用和禁用之间摄像机的行为是一致的。
  1. Update方法
  2. void Update()
  3. {
  4.     if (TpsFollow != null && FollowTarget != null)
  5.     {
  6.         var xRot = FollowTarget.rotation.eulerAngles.x;
  7.         if (xRot > 180)
  8.             xRot -= 360;
  9.         var t = (xRot - MinAngle) / (MaxAngle - MinAngle);
  10.         TpsFollow.CameraDistance = BaseDistance * DistanceScale.Evaluate(t);
  11.     }
  12. }

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中恢复,从而确保即使组件被启用和禁用,摄像机的行为也不会受到影响。
  • 这个功能可以用于创建更加自然和响应玩家动作的第三人称视角跟随效果。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值