CinemachineSameAsFollowTarget.cs 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. using Cinemachine.Utility;
  2. using UnityEngine;
  3. using UnityEngine.Serialization;
  4. namespace Cinemachine
  5. {
  6. /// <summary>
  7. /// This is a CinemachineComponent in the Aim section of the component pipeline.
  8. /// Its job is to match the orientation of the Follow target.
  9. /// </summary>
  10. [DocumentationSorting(DocumentationSortingAttribute.Level.UserRef)]
  11. [AddComponentMenu("")] // Don't display in add component menu
  12. [SaveDuringPlay]
  13. public class CinemachineSameAsFollowTarget : CinemachineComponentBase
  14. {
  15. /// <summary>
  16. /// How much time it takes for the aim to catch up to the target's rotation
  17. /// </summary>
  18. [Tooltip("How much time it takes for the aim to catch up to the target's rotation")]
  19. [FormerlySerializedAs("m_AngularDamping")]
  20. public float m_Damping = 0;
  21. Quaternion m_PreviousReferenceOrientation = Quaternion.identity;
  22. /// <summary>True if component is enabled and has a Follow target defined</summary>
  23. public override bool IsValid { get { return enabled && FollowTarget != null; } }
  24. /// <summary>Get the Cinemachine Pipeline stage that this component implements.
  25. /// Always returns the Aim stage</summary>
  26. public override CinemachineCore.Stage Stage { get { return CinemachineCore.Stage.Aim; } }
  27. /// <summary>
  28. /// Report maximum damping time needed for this component.
  29. /// </summary>
  30. /// <returns>Highest damping setting in this component</returns>
  31. public override float GetMaxDampTime() { return m_Damping; }
  32. /// <summary>Orients the camera to match the Follow target's orientation</summary>
  33. /// <param name="curState">The current camera state</param>
  34. /// <param name="deltaTime">Not used.</param>
  35. public override void MutateCameraState(ref CameraState curState, float deltaTime)
  36. {
  37. if (!IsValid)
  38. return;
  39. Quaternion dampedOrientation = FollowTargetRotation;
  40. if (deltaTime >= 0)
  41. {
  42. float t = VirtualCamera.DetachedFollowTargetDamp(1, m_Damping, deltaTime);
  43. dampedOrientation = Quaternion.Slerp(
  44. m_PreviousReferenceOrientation, FollowTargetRotation, t);
  45. }
  46. m_PreviousReferenceOrientation = dampedOrientation;
  47. curState.RawOrientation = dampedOrientation;
  48. }
  49. }
  50. }