CinemachineHardLockToTarget.cs 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. using Cinemachine.Utility;
  2. using UnityEngine;
  3. namespace Cinemachine
  4. {
  5. /// <summary>
  6. /// This is a CinemachineComponent in the Aim section of the component pipeline.
  7. /// Its job is to place the camera on the Follow Target.
  8. /// </summary>
  9. [DocumentationSorting(DocumentationSortingAttribute.Level.UserRef)]
  10. [AddComponentMenu("")] // Don't display in add component menu
  11. [SaveDuringPlay]
  12. public class CinemachineHardLockToTarget : CinemachineComponentBase
  13. {
  14. /// <summary>
  15. /// How much time it takes for the position to catch up to the target's position
  16. /// </summary>
  17. [Tooltip("How much time it takes for the position to catch up to the target's position")]
  18. public float m_Damping = 0;
  19. Vector3 m_PreviousTargetPosition;
  20. /// <summary>True if component is enabled and has a LookAt defined</summary>
  21. public override bool IsValid { get { return enabled && FollowTarget != null; } }
  22. /// <summary>Get the Cinemachine Pipeline stage that this component implements.
  23. /// Always returns the Aim stage</summary>
  24. public override CinemachineCore.Stage Stage { get { return CinemachineCore.Stage.Body; } }
  25. /// <summary>
  26. /// Report maximum damping time needed for this component.
  27. /// </summary>
  28. /// <returns>Highest damping setting in this component</returns>
  29. public override float GetMaxDampTime() { return m_Damping; }
  30. /// <summary>Applies the composer rules and orients the camera accordingly</summary>
  31. /// <param name="curState">The current camera state</param>
  32. /// <param name="deltaTime">Used for calculating damping. If less than
  33. /// zero, then target will snap to the center of the dead zone.</param>
  34. public override void MutateCameraState(ref CameraState curState, float deltaTime)
  35. {
  36. if (!IsValid)
  37. return;
  38. Vector3 dampedPos = FollowTargetPosition;
  39. if (deltaTime >= 0)
  40. dampedPos = m_PreviousTargetPosition + VirtualCamera.DetachedFollowTargetDamp(
  41. dampedPos - m_PreviousTargetPosition, m_Damping, deltaTime);
  42. m_PreviousTargetPosition = dampedPos;
  43. curState.RawPosition = dampedPos;
  44. }
  45. }
  46. }