CinemachineHardLookAt.cs 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. using UnityEngine;
  2. namespace Cinemachine
  3. {
  4. /// <summary>
  5. /// This is a CinemachineComponent in the Aim section of the component pipeline.
  6. /// Its job is to aim the camera hard at the LookAt target.
  7. /// </summary>
  8. [DocumentationSorting(DocumentationSortingAttribute.Level.UserRef)]
  9. [AddComponentMenu("")] // Don't display in add component menu
  10. [SaveDuringPlay]
  11. public class CinemachineHardLookAt : CinemachineComponentBase
  12. {
  13. /// <summary>True if component is enabled and has a LookAt defined</summary>
  14. public override bool IsValid { get { return enabled && LookAtTarget != null; } }
  15. /// <summary>Get the Cinemachine Pipeline stage that this component implements.
  16. /// Always returns the Aim stage</summary>
  17. public override CinemachineCore.Stage Stage { get { return CinemachineCore.Stage.Aim; } }
  18. /// <summary>Applies the composer rules and orients the camera accordingly</summary>
  19. /// <param name="curState">The current camera state</param>
  20. /// <param name="deltaTime">Used for calculating damping. If less than
  21. /// zero, then target will snap to the center of the dead zone.</param>
  22. public override void MutateCameraState(ref CameraState curState, float deltaTime)
  23. {
  24. if (IsValid && curState.HasLookAt)
  25. {
  26. Vector3 dir = (curState.ReferenceLookAt - curState.CorrectedPosition);
  27. if (dir.magnitude > Epsilon)
  28. {
  29. if (Vector3.Cross(dir.normalized, curState.ReferenceUp).magnitude < Epsilon)
  30. curState.RawOrientation = Quaternion.FromToRotation(Vector3.forward, dir);
  31. else
  32. curState.RawOrientation = Quaternion.LookRotation(dir, curState.ReferenceUp);
  33. }
  34. }
  35. }
  36. }
  37. }