CinemachinePOV.cs 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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 aim the camera in response to the user's mouse or joystick input.
  8. ///
  9. /// The composer does not change the camera's position. It will only pan and tilt the
  10. /// camera where it is, in order to get the desired framing. To move the camera, you have
  11. /// to use the virtual camera's Body section.
  12. /// </summary>
  13. [DocumentationSorting(DocumentationSortingAttribute.Level.UserRef)]
  14. [AddComponentMenu("")] // Don't display in add component menu
  15. [SaveDuringPlay]
  16. public class CinemachinePOV : CinemachineComponentBase
  17. {
  18. /// <summary>
  19. /// Defines the recentering target: Recentering goes here
  20. /// </summary>
  21. public enum RecenterTargetMode
  22. {
  23. /// <summary>
  24. /// Just go to 0
  25. /// </summary>
  26. None,
  27. /// <summary>
  28. /// Axis angles are relative to Follow target's forward
  29. /// </summary>
  30. FollowTargetForward,
  31. /// <summary>
  32. /// Axis angles are relative to LookAt target's forward
  33. /// </summary>
  34. LookAtTargetForward
  35. }
  36. /// <summary>
  37. /// Defines the recentering target: recentering goes here
  38. /// </summary>
  39. public RecenterTargetMode m_RecenterTarget = RecenterTargetMode.None;
  40. /// <summary>The Vertical axis. Value is -90..90. Controls the vertical orientation</summary>
  41. [Tooltip("The Vertical axis. Value is -90..90. Controls the vertical orientation")]
  42. [AxisStateProperty]
  43. public AxisState m_VerticalAxis = new AxisState(-70, 70, false, false, 300f, 0.1f, 0.1f, "Mouse Y", true);
  44. /// <summary>Controls how automatic recentering of the Vertical axis is accomplished</summary>
  45. [Tooltip("Controls how automatic recentering of the Vertical axis is accomplished")]
  46. public AxisState.Recentering m_VerticalRecentering = new AxisState.Recentering(false, 1, 2);
  47. /// <summary>The Horizontal axis. Value is -180..180. Controls the horizontal orientation</summary>
  48. [Tooltip("The Horizontal axis. Value is -180..180. Controls the horizontal orientation")]
  49. [AxisStateProperty]
  50. public AxisState m_HorizontalAxis = new AxisState(-180, 180, true, false, 300f, 0.1f, 0.1f, "Mouse X", false);
  51. /// <summary>Controls how automatic recentering of the Horizontal axis is accomplished</summary>
  52. [Tooltip("Controls how automatic recentering of the Horizontal axis is accomplished")]
  53. public AxisState.Recentering m_HorizontalRecentering = new AxisState.Recentering(false, 1, 2);
  54. /// <summary>Obsolete - no longer used</summary>
  55. [HideInInspector]
  56. [Tooltip("Obsolete - no longer used")]
  57. public bool m_ApplyBeforeBody;
  58. /// <summary>True if component is enabled and has a LookAt defined</summary>
  59. public override bool IsValid { get { return enabled; } }
  60. /// <summary>Get the Cinemachine Pipeline stage that this component implements.
  61. /// Always returns the Aim stage</summary>
  62. public override CinemachineCore.Stage Stage { get { return CinemachineCore.Stage.Aim; } }
  63. private void OnValidate()
  64. {
  65. m_VerticalAxis.Validate();
  66. m_VerticalRecentering.Validate();
  67. m_HorizontalAxis.Validate();
  68. m_HorizontalRecentering.Validate();
  69. }
  70. private void OnEnable()
  71. {
  72. UpdateInputAxisProvider();
  73. }
  74. /// <summary>
  75. /// API for the inspector. Internal use only
  76. /// </summary>
  77. public void UpdateInputAxisProvider()
  78. {
  79. m_HorizontalAxis.SetInputAxisProvider(0, null);
  80. m_VerticalAxis.SetInputAxisProvider(1, null);
  81. if (VirtualCamera != null)
  82. {
  83. var provider = VirtualCamera.GetInputAxisProvider();
  84. if (provider != null)
  85. {
  86. m_HorizontalAxis.SetInputAxisProvider(0, provider);
  87. m_VerticalAxis.SetInputAxisProvider(1, provider);
  88. }
  89. }
  90. }
  91. /// <summary>Does nothing</summary>
  92. /// <param name="state"></param>
  93. /// <param name="deltaTime"></param>
  94. public override void PrePipelineMutateCameraState(ref CameraState state, float deltaTime) {}
  95. /// <summary>Applies the axis values and orients the camera accordingly</summary>
  96. /// <param name="curState">The current camera state</param>
  97. /// <param name="deltaTime">Used for calculating damping. Not used.</param>
  98. public override void MutateCameraState(ref CameraState curState, float deltaTime)
  99. {
  100. if (!IsValid)
  101. return;
  102. // Only read joystick when game is playing
  103. if (deltaTime >= 0 && CinemachineCore.Instance.IsLive(VirtualCamera))
  104. {
  105. if (m_HorizontalAxis.Update(deltaTime))
  106. m_HorizontalRecentering.CancelRecentering();
  107. if (m_VerticalAxis.Update(deltaTime))
  108. m_VerticalRecentering.CancelRecentering();
  109. var recenterTarget = GetRecenterTarget();
  110. m_HorizontalRecentering.DoRecentering(ref m_HorizontalAxis, deltaTime, recenterTarget.x);
  111. m_VerticalRecentering.DoRecentering(ref m_VerticalAxis, deltaTime, recenterTarget.y);
  112. }
  113. // If we have a transform parent, then apply POV in the local space of the parent
  114. Quaternion rot = Quaternion.Euler(m_VerticalAxis.Value, m_HorizontalAxis.Value, 0);
  115. Transform parent = VirtualCamera.transform.parent;
  116. if (parent != null)
  117. rot = parent.rotation * rot;
  118. rot = Quaternion.FromToRotation(Vector3.up, curState.ReferenceUp) * rot;
  119. curState.RawOrientation = rot;
  120. }
  121. /// <summary>
  122. /// Get the horizonmtal and vertical angles that correspong to "at rest" position.
  123. /// </summary>
  124. /// <returns>X is horizontal angle (rot Y) and Y is vertical angle (rot X)</returns>
  125. public Vector2 GetRecenterTarget()
  126. {
  127. Transform t = null;
  128. switch (m_RecenterTarget)
  129. {
  130. case RecenterTargetMode.FollowTargetForward: t = VirtualCamera.Follow; break;
  131. case RecenterTargetMode.LookAtTargetForward: t = VirtualCamera.LookAt; break;
  132. default: break;
  133. }
  134. if (t != null)
  135. {
  136. var fwd = t.forward;
  137. Transform parent = VirtualCamera.transform.parent;
  138. if (parent != null)
  139. fwd = parent.rotation * fwd;
  140. var v = Quaternion.FromToRotation(Vector3.forward, fwd).eulerAngles;
  141. return new Vector2(NormalizeAngle(v.y), NormalizeAngle(v.x));
  142. }
  143. return Vector2.zero;
  144. }
  145. // Normalize angle value to [-180, 180] degrees.
  146. static float NormalizeAngle(float angle)
  147. {
  148. return ((angle + 180) % 360) - 180;
  149. }
  150. /// <summary>
  151. /// Force the virtual camera to assume a given position and orientation.
  152. /// Procedural placement then takes over
  153. /// </summary>
  154. /// <param name="pos">Worldspace pposition to take</param>
  155. /// <param name="rot">Worldspace orientation to take</param>
  156. public override void ForceCameraPosition(Vector3 pos, Quaternion rot)
  157. {
  158. SetAxesForRotation(rot);
  159. }
  160. /// <summary>Notification that this virtual camera is going live.
  161. /// Base class implementation does nothing.</summary>
  162. /// <param name="fromCam">The camera being deactivated. May be null.</param>
  163. /// <param name="worldUp">Default world Up, set by the CinemachineBrain</param>
  164. /// <param name="deltaTime">Delta time for time-based effects (ignore if less than or equal to 0)</param>
  165. /// <param name="transitionParams">Transition settings for this vcam</param>
  166. /// <returns>True if the vcam should do an internal update as a result of this call</returns>
  167. public override bool OnTransitionFromCamera(
  168. ICinemachineCamera fromCam, Vector3 worldUp, float deltaTime,
  169. ref CinemachineVirtualCameraBase.TransitionParams transitionParams)
  170. {
  171. m_HorizontalRecentering.DoRecentering(ref m_HorizontalAxis, -1, 0);
  172. m_VerticalRecentering.DoRecentering(ref m_VerticalAxis, -1, 0);
  173. m_HorizontalRecentering.CancelRecentering();
  174. m_VerticalRecentering.CancelRecentering();
  175. if (fromCam != null && transitionParams.m_InheritPosition)
  176. {
  177. SetAxesForRotation(fromCam.State.RawOrientation);
  178. return true;
  179. }
  180. return false;
  181. }
  182. void SetAxesForRotation(Quaternion targetRot)
  183. {
  184. Vector3 up = VcamState.ReferenceUp;
  185. Vector3 fwd = Vector3.forward;
  186. Transform parent = VirtualCamera.transform.parent;
  187. if (parent != null)
  188. fwd = parent.rotation * fwd;
  189. m_HorizontalAxis.Value = 0;
  190. m_HorizontalAxis.Reset();
  191. Vector3 targetFwd = targetRot * Vector3.forward;
  192. Vector3 a = fwd.ProjectOntoPlane(up);
  193. Vector3 b = targetFwd.ProjectOntoPlane(up);
  194. if (!a.AlmostZero() && !b.AlmostZero())
  195. m_HorizontalAxis.Value = Vector3.SignedAngle(a, b, up);
  196. m_VerticalAxis.Value = 0;
  197. m_VerticalAxis.Reset();
  198. fwd = Quaternion.AngleAxis(m_HorizontalAxis.Value, up) * fwd;
  199. Vector3 right = Vector3.Cross(up, fwd);
  200. if (!right.AlmostZero())
  201. m_VerticalAxis.Value = Vector3.SignedAngle(fwd, targetFwd, right);
  202. }
  203. }
  204. }