CinemachineOrbitalTransposer.cs 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478
  1. #if !UNITY_2019_3_OR_NEWER
  2. #define CINEMACHINE_PHYSICS
  3. #define CINEMACHINE_PHYSICS_2D
  4. #endif
  5. using System;
  6. using UnityEngine;
  7. using Cinemachine.Utility;
  8. using UnityEngine.Serialization;
  9. namespace Cinemachine
  10. {
  11. /// <summary>
  12. /// This is a CinemachineComponent in the the Body section of the component pipeline.
  13. /// Its job is to position the camera in a variable relationship to a the vcam's
  14. /// Follow target object, with offsets and damping.
  15. ///
  16. /// This component is typically used to implement a camera that follows its target.
  17. /// It can accept player input from an input device, which allows the player to
  18. /// dynamically control the relationship between the camera and the target,
  19. /// for example with a joystick.
  20. ///
  21. /// The OrbitalTransposer introduces the concept of __Heading__, which is the direction
  22. /// in which the target is moving, and the OrbitalTransposer will attempt to position
  23. /// the camera in relationship to the heading, which is by default directly behind the target.
  24. /// You can control the default relationship by adjusting the Heading Bias setting.
  25. ///
  26. /// If you attach an input controller to the OrbitalTransposer, then the player can also
  27. /// control the way the camera positions itself in relation to the target heading. This allows
  28. /// the camera to move to any spot on an orbit around the target.
  29. /// </summary>
  30. [DocumentationSorting(DocumentationSortingAttribute.Level.UserRef)]
  31. [AddComponentMenu("")] // Don't display in add component menu
  32. [SaveDuringPlay]
  33. public class CinemachineOrbitalTransposer : CinemachineTransposer
  34. {
  35. /// <summary>
  36. /// How the "forward" direction is defined. Orbital offset is in relation to the forward
  37. /// direction.
  38. /// </summary>
  39. [DocumentationSorting(DocumentationSortingAttribute.Level.UserRef)]
  40. [Serializable]
  41. public struct Heading
  42. {
  43. /// <summary>
  44. /// Sets the algorithm for determining the target's heading for purposes
  45. /// of re-centering the camera
  46. /// </summary>
  47. [DocumentationSorting(DocumentationSortingAttribute.Level.UserRef)]
  48. public enum HeadingDefinition
  49. {
  50. /// <summary>
  51. /// Target heading calculated from the difference between its position on
  52. /// the last update and current frame.
  53. /// </summary>
  54. PositionDelta,
  55. /// <summary>
  56. /// Target heading calculated from its <b>Rigidbody</b>'s velocity.
  57. /// If no <b>Rigidbody</b> exists, it will fall back
  58. /// to HeadingDerivationMode.PositionDelta
  59. /// </summary>
  60. Velocity,
  61. /// <summary>
  62. /// Target heading calculated from the Target <b>Transform</b>'s euler Y angle
  63. /// </summary>
  64. TargetForward,
  65. /// <summary>
  66. /// Default heading is a constant world space heading.
  67. /// </summary>
  68. WorldForward,
  69. }
  70. /// <summary>The method by which the 'default heading' is calculated if
  71. /// recentering to target heading is enabled</summary>
  72. [FormerlySerializedAs("m_HeadingDefinition")]
  73. [Tooltip("How 'forward' is defined. The camera will be placed by default behind the target. "
  74. + "PositionDelta will consider 'forward' to be the direction in which the target is moving.")]
  75. public HeadingDefinition m_Definition;
  76. /// <summary>Size of the velocity sampling window for target heading filter.
  77. /// Used only if deriving heading from target's movement</summary>
  78. [Range(0, 10)]
  79. [Tooltip("Size of the velocity sampling window for target heading filter. This filters out "
  80. + "irregularities in the target's movement. Used only if deriving heading from target's "
  81. + "movement (PositionDelta or Velocity)")]
  82. public int m_VelocityFilterStrength;
  83. /// <summary>Additional Y rotation applied to the target heading.
  84. /// When this value is 0, the camera will be placed behind the target</summary>
  85. [Range(-180f, 180f)]
  86. [FormerlySerializedAs("m_HeadingBias")]
  87. [Tooltip("Where the camera is placed when the X-axis value is zero. This is a rotation in "
  88. + "degrees around the Y axis. When this value is 0, the camera will be placed behind "
  89. + "the target. Nonzero offsets will rotate the zero position around the target.")]
  90. public float m_Bias;
  91. /// <summary>Constructor</summary>
  92. /// <param name="def">The heading definition</param>
  93. /// <param name="filterStrength">The strength of the heading filter</param>
  94. /// <param name="bias">The heading bias</param>
  95. public Heading(HeadingDefinition def, int filterStrength, float bias)
  96. {
  97. m_Definition = def;
  98. m_VelocityFilterStrength = filterStrength;
  99. m_Bias = bias;
  100. }
  101. };
  102. /// <summary>The definition of Forward. Camera will follow behind.</summary>
  103. [Space]
  104. [OrbitalTransposerHeadingProperty]
  105. [Tooltip("The definition of Forward. Camera will follow behind.")]
  106. public Heading m_Heading = new Heading(Heading.HeadingDefinition.TargetForward, 4, 0);
  107. /// <summary>Parameters that control Automating Heading Recentering</summary>
  108. [Tooltip("Automatic heading recentering. The settings here defines how the camera "
  109. + "will reposition itself in the absence of player input.")]
  110. public AxisState.Recentering m_RecenterToTargetHeading = new AxisState.Recentering(true, 1, 2);
  111. /// <summary>Axis representing the current heading. Value is in degrees
  112. /// and represents a rotation about the up vector</summary>
  113. [Tooltip("Heading Control. The settings here control the behaviour of the camera "
  114. + "in response to the player's input.")]
  115. [AxisStateProperty]
  116. public AxisState m_XAxis = new AxisState(-180, 180, true, false, 300f, 0.1f, 0.1f, "Mouse X", true);
  117. /// <summary>Legacy support</summary>
  118. [SerializeField] [HideInInspector] [FormerlySerializedAs("m_Radius")] private float m_LegacyRadius = float.MaxValue;
  119. [SerializeField] [HideInInspector] [FormerlySerializedAs("m_HeightOffset")] private float m_LegacyHeightOffset = float.MaxValue;
  120. [SerializeField] [HideInInspector] [FormerlySerializedAs("m_HeadingBias")] private float m_LegacyHeadingBias = float.MaxValue;
  121. /// <summary>Legacy support for old serialized versions</summary>
  122. protected override void OnValidate()
  123. {
  124. // Upgrade after a legacy deserialize
  125. if (m_LegacyRadius != float.MaxValue
  126. && m_LegacyHeightOffset != float.MaxValue
  127. && m_LegacyHeadingBias != float.MaxValue)
  128. {
  129. m_FollowOffset = new Vector3(0, m_LegacyHeightOffset, -m_LegacyRadius);
  130. m_LegacyHeightOffset = m_LegacyRadius = float.MaxValue;
  131. m_Heading.m_Bias = m_LegacyHeadingBias;
  132. m_XAxis.m_MaxSpeed /= 10;
  133. m_XAxis.m_AccelTime /= 10;
  134. m_XAxis.m_DecelTime /= 10;
  135. m_LegacyHeadingBias = float.MaxValue;
  136. int heading = (int)m_Heading.m_Definition;
  137. if (m_RecenterToTargetHeading.LegacyUpgrade(ref heading, ref m_Heading.m_VelocityFilterStrength))
  138. m_Heading.m_Definition = (Heading.HeadingDefinition)heading;
  139. }
  140. m_XAxis.Validate();
  141. m_RecenterToTargetHeading.Validate();
  142. base.OnValidate();
  143. }
  144. /// <summary>
  145. /// Drive the x-axis setting programmatically.
  146. /// Automatic heading updating will be disabled.
  147. /// </summary>
  148. [HideInInspector, NoSaveDuringPlay]
  149. public bool m_HeadingIsSlave = false;
  150. /// <summary>
  151. /// Delegate that allows the the m_XAxis object to be replaced with another one.
  152. /// </summary>
  153. internal delegate float UpdateHeadingDelegate(
  154. CinemachineOrbitalTransposer orbital, float deltaTime, Vector3 up);
  155. /// <summary>
  156. /// Delegate that allows the the XAxis object to be replaced with another one.
  157. /// To use it, just call orbital.UpdateHeading() with a reference to a
  158. /// private AxisState object, and that AxisState object will be updated and
  159. /// used to calculate the heading.
  160. /// </summary>
  161. internal UpdateHeadingDelegate HeadingUpdater
  162. = (CinemachineOrbitalTransposer orbital, float deltaTime, Vector3 up) => {
  163. return orbital.UpdateHeading(
  164. deltaTime, up, ref orbital.m_XAxis,
  165. ref orbital.m_RecenterToTargetHeading,
  166. CinemachineCore.Instance.IsLive(orbital.VirtualCamera));
  167. };
  168. /// <summary>
  169. /// Update the X axis and calculate the heading. This can be called by a delegate
  170. /// with a custom axis. Note that this method is obsolete.
  171. /// </summary>
  172. /// <param name="deltaTime">Used for damping. If less than 0, no damping is done.</param>
  173. /// <param name="up">World Up, set by the CinemachineBrain</param>
  174. /// <param name="axis"></param>
  175. /// <returns>Axis value</returns>
  176. public float UpdateHeading(float deltaTime, Vector3 up, ref AxisState axis)
  177. {
  178. return UpdateHeading(deltaTime, up, ref axis, ref m_RecenterToTargetHeading, true);
  179. }
  180. /// <summary>
  181. /// Update the X axis and calculate the heading. This can be called by a delegate
  182. /// with a custom axis.
  183. /// </summary>
  184. /// <param name="deltaTime">Used for damping. If less than 0, no damping is done.</param>
  185. /// <param name="up">World Up, set by the CinemachineBrain</param>
  186. /// <param name="axis"></param>
  187. /// <param name="recentering"></param>
  188. /// <param name="isLive">true if the vcam is live</param>
  189. /// <returns>Axis value</returns>
  190. public float UpdateHeading(
  191. float deltaTime, Vector3 up, ref AxisState axis,
  192. ref AxisState.Recentering recentering, bool isLive)
  193. {
  194. if (m_BindingMode == BindingMode.SimpleFollowWithWorldUp)
  195. {
  196. axis.m_MinValue = -180;
  197. axis.m_MaxValue = 180;
  198. }
  199. // Only read joystick when game is playing
  200. if (deltaTime < 0 || !VirtualCamera.PreviousStateIsValid || !isLive)
  201. {
  202. axis.Reset();
  203. recentering.CancelRecentering();
  204. }
  205. else if (axis.Update(deltaTime))
  206. recentering.CancelRecentering();
  207. if (m_BindingMode == BindingMode.SimpleFollowWithWorldUp)
  208. {
  209. float finalHeading = axis.Value;
  210. axis.Value = 0;
  211. return finalHeading;
  212. }
  213. float targetHeading = GetTargetHeading(axis.Value, GetReferenceOrientation(up));
  214. recentering.DoRecentering(ref axis, deltaTime, targetHeading);
  215. return axis.Value;
  216. }
  217. private void OnEnable()
  218. {
  219. // GML todo: do we really need this?
  220. PreviousTarget = null;
  221. mLastTargetPosition = Vector3.zero;
  222. UpdateInputAxisProvider();
  223. }
  224. /// <summary>
  225. /// API for the inspector. Internal use only
  226. /// </summary>
  227. public void UpdateInputAxisProvider()
  228. {
  229. m_XAxis.SetInputAxisProvider(0, null);
  230. if (!m_HeadingIsSlave && VirtualCamera != null)
  231. {
  232. var provider = VirtualCamera.GetInputAxisProvider();
  233. if (provider != null)
  234. m_XAxis.SetInputAxisProvider(0, provider);
  235. }
  236. }
  237. private Vector3 mLastTargetPosition = Vector3.zero;
  238. private HeadingTracker mHeadingTracker;
  239. #if CINEMACHINE_PHYSICS
  240. private Rigidbody mTargetRigidBody = null;
  241. #endif
  242. private Transform PreviousTarget { get; set; }
  243. private Vector3 mLastCameraPosition;
  244. /// <summary>This is called to notify the us that a target got warped,
  245. /// so that we can update its internal state to make the camera
  246. /// also warp seamlessy.</summary>
  247. /// <param name="target">The object that was warped</param>
  248. /// <param name="positionDelta">The amount the target's position changed</param>
  249. public override void OnTargetObjectWarped(Transform target, Vector3 positionDelta)
  250. {
  251. base.OnTargetObjectWarped(target, positionDelta);
  252. if (target == FollowTarget)
  253. {
  254. mLastTargetPosition += positionDelta;
  255. mLastCameraPosition += positionDelta;
  256. }
  257. }
  258. /// <summary>
  259. /// Force the virtual camera to assume a given position and orientation
  260. /// </summary>
  261. /// <param name="pos">Worldspace pposition to take</param>
  262. /// <param name="rot">Worldspace orientation to take</param>
  263. public override void ForceCameraPosition(Vector3 pos, Quaternion rot)
  264. {
  265. base.ForceCameraPosition(pos, rot);
  266. mLastCameraPosition = pos;
  267. m_XAxis.Value = GetAxisClosestValue(pos, VirtualCamera.State.ReferenceUp);
  268. }
  269. /// <summary>Notification that this virtual camera is going live.
  270. /// Base class implementation does nothing.</summary>
  271. /// <param name="fromCam">The camera being deactivated. May be null.</param>
  272. /// <param name="worldUp">Default world Up, set by the CinemachineBrain</param>
  273. /// <param name="deltaTime">Delta time for time-based effects (ignore if less than or equal to 0)</param>
  274. /// <param name="transitionParams">Transition settings for this vcam</param>
  275. /// <returns>True if the vcam should do an internal update as a result of this call</returns>
  276. public override bool OnTransitionFromCamera(
  277. ICinemachineCamera fromCam, Vector3 worldUp, float deltaTime,
  278. ref CinemachineVirtualCameraBase.TransitionParams transitionParams)
  279. {
  280. m_RecenterToTargetHeading.DoRecentering(ref m_XAxis, -1, 0);
  281. m_RecenterToTargetHeading.CancelRecentering();
  282. if (fromCam != null //&& fromCam.Follow == FollowTarget
  283. && m_BindingMode != CinemachineTransposer.BindingMode.SimpleFollowWithWorldUp
  284. && transitionParams.m_InheritPosition)
  285. {
  286. m_XAxis.Value = GetAxisClosestValue(fromCam.State.RawPosition, worldUp);
  287. return true;
  288. }
  289. return false;
  290. }
  291. /// <summary>
  292. /// What axis value would we need to get as close as possible to the desired cameraPos?
  293. /// </summary>
  294. /// <param name="cameraPos">camera position we would like to approximate</param>
  295. /// <param name="up">world up</param>
  296. /// <returns>The best value to put into the X axis, to approximate the desired camera pos</returns>
  297. public float GetAxisClosestValue(Vector3 cameraPos, Vector3 up)
  298. {
  299. Quaternion orient = GetReferenceOrientation(up);
  300. Vector3 fwd = (orient * Vector3.forward).ProjectOntoPlane(up);
  301. if (!fwd.AlmostZero() && FollowTarget != null)
  302. {
  303. // Get the base camera placement
  304. float heading = 0;
  305. if (m_BindingMode != BindingMode.SimpleFollowWithWorldUp)
  306. heading += m_Heading.m_Bias;
  307. orient = orient * Quaternion.AngleAxis(heading, up);
  308. Vector3 targetPos = FollowTargetPosition;
  309. Vector3 pos = targetPos + orient * EffectiveOffset;
  310. Vector3 a = (pos - targetPos).ProjectOntoPlane(up);
  311. Vector3 b = (cameraPos - targetPos).ProjectOntoPlane(up);
  312. return Vector3.SignedAngle(a, b, up);
  313. }
  314. return LastHeading; // Can't calculate, stay conservative
  315. }
  316. float LastHeading { get; set; }
  317. /// <summary>Positions the virtual camera according to the transposer rules.</summary>
  318. /// <param name="curState">The current camera state</param>
  319. /// <param name="deltaTime">Used for damping. If less than 0, no damping is done.</param>
  320. public override void MutateCameraState(ref CameraState curState, float deltaTime)
  321. {
  322. InitPrevFrameStateInfo(ref curState, deltaTime);
  323. // Update the heading
  324. if (FollowTarget != PreviousTarget)
  325. {
  326. PreviousTarget = FollowTarget;
  327. #if CINEMACHINE_PHYSICS
  328. mTargetRigidBody = (PreviousTarget == null) ? null : PreviousTarget.GetComponent<Rigidbody>();
  329. #endif
  330. mLastTargetPosition = (PreviousTarget == null) ? Vector3.zero : PreviousTarget.position;
  331. mHeadingTracker = null;
  332. }
  333. LastHeading = HeadingUpdater(this, deltaTime, curState.ReferenceUp);
  334. float heading = LastHeading;
  335. if (IsValid)
  336. {
  337. // Calculate the heading
  338. if (m_BindingMode != BindingMode.SimpleFollowWithWorldUp)
  339. heading += m_Heading.m_Bias;
  340. Quaternion headingRot = Quaternion.AngleAxis(heading, Vector3.up);
  341. Vector3 rawOffset = EffectiveOffset;
  342. Vector3 offset = headingRot * rawOffset;
  343. // Track the target, with damping
  344. TrackTarget(deltaTime, curState.ReferenceUp, offset, out Vector3 pos, out Quaternion orient);
  345. // Place the camera
  346. offset = orient * offset;
  347. curState.ReferenceUp = orient * Vector3.up;
  348. // Respect minimum target distance on XZ plane
  349. var targetPosition = FollowTargetPosition;
  350. pos += GetOffsetForMinimumTargetDistance(
  351. pos, offset, curState.RawOrientation * Vector3.forward,
  352. curState.ReferenceUp, targetPosition);
  353. curState.RawPosition = pos + offset;
  354. if (deltaTime >= 0 && VirtualCamera.PreviousStateIsValid)
  355. {
  356. var lookAt = targetPosition;
  357. if (LookAtTarget != null)
  358. lookAt = LookAtTargetPosition;
  359. var dir0 = mLastCameraPosition - lookAt;
  360. var dir1 = curState.RawPosition - lookAt;
  361. if (dir0.sqrMagnitude > 0.01f && dir1.sqrMagnitude > 0.01f)
  362. curState.PositionDampingBypass = UnityVectorExtensions.SafeFromToRotation(
  363. dir0, dir1, curState.ReferenceUp).eulerAngles;
  364. }
  365. mLastTargetPosition = targetPosition;
  366. mLastCameraPosition = curState.RawPosition;
  367. }
  368. }
  369. /// <summary>Internal API for the Inspector Editor, so it can draw a marker at the target</summary>
  370. /// <param name="worldUp">Current effective world up</param>
  371. /// <returns>The position of the Follow target</returns>
  372. public override Vector3 GetTargetCameraPosition(Vector3 worldUp)
  373. {
  374. if (!IsValid)
  375. return Vector3.zero;
  376. float heading = LastHeading;
  377. if (m_BindingMode != BindingMode.SimpleFollowWithWorldUp)
  378. heading += m_Heading.m_Bias;
  379. Quaternion orient = Quaternion.AngleAxis(heading, Vector3.up);
  380. orient = GetReferenceOrientation(worldUp) * orient;
  381. var pos = orient * EffectiveOffset;
  382. pos += mLastTargetPosition;
  383. return pos;
  384. }
  385. // Make sure this is calld only once per frame
  386. private float GetTargetHeading(float currentHeading, Quaternion targetOrientation)
  387. {
  388. if (m_BindingMode == BindingMode.SimpleFollowWithWorldUp)
  389. return 0;
  390. if (FollowTarget == null)
  391. return currentHeading;
  392. var headingDef = m_Heading.m_Definition;
  393. #if CINEMACHINE_PHYSICS
  394. if (headingDef == Heading.HeadingDefinition.Velocity && mTargetRigidBody == null)
  395. headingDef = Heading.HeadingDefinition.PositionDelta;
  396. #endif
  397. Vector3 velocity = Vector3.zero;
  398. switch (headingDef)
  399. {
  400. case Heading.HeadingDefinition.Velocity:
  401. #if CINEMACHINE_PHYSICS
  402. velocity = mTargetRigidBody.velocity;
  403. break;
  404. #endif
  405. case Heading.HeadingDefinition.PositionDelta:
  406. velocity = FollowTargetPosition - mLastTargetPosition;
  407. break;
  408. case Heading.HeadingDefinition.TargetForward:
  409. velocity = FollowTargetRotation * Vector3.forward;
  410. break;
  411. default:
  412. case Heading.HeadingDefinition.WorldForward:
  413. return 0;
  414. }
  415. // Process the velocity and derive the heading from it.
  416. Vector3 up = targetOrientation * Vector3.up;
  417. velocity = velocity.ProjectOntoPlane(up);
  418. if (headingDef != Heading.HeadingDefinition.TargetForward)
  419. {
  420. int filterSize = m_Heading.m_VelocityFilterStrength * 5;
  421. if (mHeadingTracker == null || mHeadingTracker.FilterSize != filterSize)
  422. mHeadingTracker = new HeadingTracker(filterSize);
  423. mHeadingTracker.DecayHistory();
  424. if (!velocity.AlmostZero())
  425. mHeadingTracker.Add(velocity);
  426. velocity = mHeadingTracker.GetReliableHeading();
  427. }
  428. if (!velocity.AlmostZero())
  429. return UnityVectorExtensions.SignedAngle(
  430. targetOrientation * Vector3.forward, velocity, up);
  431. // If no reliable heading, then stay where we are.
  432. return currentHeading;
  433. }
  434. }
  435. }