RigidbodyTimeline3D.cs 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. using UnityEngine;
  2. namespace Chronos
  3. {
  4. public class RigidbodyTimeline3D : RigidbodyTimeline<Rigidbody, RigidbodyTimeline3D.Snapshot>
  5. {
  6. // Scale is disabled by default because it usually doesn't change and
  7. // would otherwise take more memory. Feel free to uncomment the lines
  8. // below if you need to record it.
  9. public struct Snapshot
  10. {
  11. public Vector3 position;
  12. public Quaternion rotation;
  13. // public Vector3 scale;
  14. public Vector3 velocity;
  15. public Vector3 angularVelocity;
  16. public float drag;
  17. public float angularDrag;
  18. public float lastPositiveTimeScale;
  19. public static Snapshot Lerp(Snapshot from, Snapshot to, float t)
  20. {
  21. return new Snapshot()
  22. {
  23. position = Vector3.Lerp(from.position, to.position, t),
  24. rotation = Quaternion.Lerp(from.rotation, to.rotation, t),
  25. // scale = Vector3.Lerp(from.scale, to.scale, t),
  26. velocity = Vector3.Lerp(from.velocity, to.velocity, t),
  27. angularVelocity = Vector3.Lerp(from.angularVelocity, to.angularVelocity, t),
  28. drag = Mathf.Lerp(from.drag, to.drag, t),
  29. angularDrag = Mathf.Lerp(from.angularDrag, to.angularDrag, t),
  30. lastPositiveTimeScale = Mathf.Lerp(from.lastPositiveTimeScale, to.lastPositiveTimeScale, t),
  31. };
  32. }
  33. }
  34. public RigidbodyTimeline3D(Timeline timeline, Rigidbody component) : base(timeline, component) { }
  35. public override void CopyProperties(Rigidbody source)
  36. {
  37. isKinematic = source.isKinematic;
  38. useGravity = source.useGravity;
  39. source.useGravity = false;
  40. }
  41. public override void FixedUpdate()
  42. {
  43. if (useGravity && !component.isKinematic && timeline.timeScale > 0)
  44. {
  45. velocity += (Physics.gravity * timeline.fixedDeltaTime);
  46. }
  47. }
  48. #region Snapshots
  49. protected override Snapshot LerpSnapshots(Snapshot from, Snapshot to, float t)
  50. {
  51. return Snapshot.Lerp(from, to, t);
  52. }
  53. // NavMeshAgents don't function correctly with manually updated transforms.
  54. // http://forum.unity3d.com/threads/317688/page-8#post-2454841
  55. private UnityEngine.AI.NavMeshAgent GetNavMeshAgentOverride()
  56. {
  57. if (timeline.navMeshAgent == null ||
  58. timeline.navMeshAgent.component == null ||
  59. !timeline.navMeshAgent.component.enabled)
  60. {
  61. return null;
  62. }
  63. return timeline.navMeshAgent.component;
  64. }
  65. protected override Snapshot CopySnapshot()
  66. {
  67. var navMeshAgent = GetNavMeshAgentOverride();
  68. return new Snapshot()
  69. {
  70. position = component.transform.position,
  71. rotation = component.transform.rotation,
  72. // scale = component.transform.localScale,
  73. velocity = navMeshAgent == null ? component.velocity : navMeshAgent.velocity,
  74. angularVelocity = component.angularVelocity,
  75. drag = component.drag,
  76. angularDrag = component.angularDrag,
  77. lastPositiveTimeScale = lastPositiveTimeScale
  78. };
  79. }
  80. protected override void ApplySnapshot(Snapshot snapshot)
  81. {
  82. var navMeshAgent = GetNavMeshAgentOverride();
  83. if (navMeshAgent == null)
  84. {
  85. component.transform.position = snapshot.position;
  86. }
  87. else
  88. {
  89. navMeshAgent.Warp(snapshot.position);
  90. }
  91. component.transform.rotation = snapshot.rotation;
  92. // component.transform.localScale = snapshot.scale;
  93. if (timeline.timeScale > 0)
  94. {
  95. if (navMeshAgent == null)
  96. {
  97. component.velocity = snapshot.velocity;
  98. }
  99. else
  100. {
  101. navMeshAgent.velocity = snapshot.velocity;
  102. }
  103. component.angularVelocity = snapshot.angularVelocity;
  104. }
  105. component.drag = snapshot.drag;
  106. component.angularDrag = snapshot.angularDrag;
  107. lastPositiveTimeScale = snapshot.lastPositiveTimeScale;
  108. }
  109. #endregion
  110. #region Components
  111. protected bool reallyUsesGravity
  112. {
  113. get { return component.useGravity; }
  114. set { component.useGravity = value; }
  115. }
  116. private bool isReallyKinematic
  117. {
  118. get { return component.isKinematic; }
  119. set { component.isKinematic = value; }
  120. }
  121. protected override bool isReallyManual
  122. {
  123. get { return isReallyKinematic; }
  124. set { isReallyKinematic = value; }
  125. }
  126. protected override float realMass
  127. {
  128. get { return component.mass; }
  129. set { component.mass = value; }
  130. }
  131. protected override Vector3 realVelocity
  132. {
  133. get { return component.velocity; }
  134. set { component.velocity = value; }
  135. }
  136. protected override Vector3 realAngularVelocity
  137. {
  138. get { return component.angularVelocity; }
  139. set { component.angularVelocity = value; }
  140. }
  141. protected override float realDrag
  142. {
  143. get { return component.drag; }
  144. set { component.drag = value; }
  145. }
  146. protected override float realAngularDrag
  147. {
  148. get { return component.angularDrag; }
  149. set { component.angularDrag = value; }
  150. }
  151. protected override void WakeUp()
  152. {
  153. component.WakeUp();
  154. }
  155. protected override bool IsSleeping()
  156. {
  157. return component.IsSleeping();
  158. }
  159. private bool _isKinematic;
  160. /// <summary>
  161. /// Determines whether the rigidbody is kinematic before time effects. Use this property instead of Rigidbody.isKinematic, which will be overwritten by the physics timer at runtime.
  162. /// </summary>
  163. public bool isKinematic
  164. {
  165. get
  166. {
  167. return _isKinematic;
  168. }
  169. set
  170. {
  171. _isKinematic = value;
  172. // Avoid frame delay if we're adding a force right after
  173. // https://support.ludiq.io/forums/1-chronos/topics/464-/
  174. if (timeline.timeScale > 0)
  175. {
  176. isReallyKinematic = value;
  177. }
  178. }
  179. }
  180. protected override bool isManual
  181. {
  182. get { return isKinematic; }
  183. }
  184. /// <summary>
  185. /// Determines whether the rigidbody uses gravity. Use this property instead of Rigidbody.useGravity, which will be overwritten by the physics timer at runtime.
  186. /// </summary>
  187. public bool useGravity { get; set; }
  188. /// <summary>
  189. /// The velocity of the rigidbody before time effects. Use this property instead of Rigidbody.velocity, which will be overwritten by the physics timer at runtime.
  190. /// </summary>
  191. public Vector3 velocity
  192. {
  193. get
  194. {
  195. if (timeline.timeScale == 0)
  196. {
  197. return zeroSnapshot.velocity;
  198. }
  199. return realVelocity / timeline.timeScale;
  200. }
  201. set
  202. {
  203. if (AssertForwardProperty("velocity", Severity.Ignore)) realVelocity = value * timeline.timeScale;
  204. }
  205. }
  206. /// <summary>
  207. /// The angular velocity of the rigidbody before time effects. Use this property instead of Rigidbody.angularVelocity, which will be overwritten by the physics timer at runtime.
  208. /// </summary>
  209. public Vector3 angularVelocity
  210. {
  211. get
  212. {
  213. if (timeline.timeScale == 0)
  214. {
  215. return zeroSnapshot.angularVelocity;
  216. }
  217. return realAngularVelocity / timeline.timeScale;
  218. }
  219. set { if (AssertForwardProperty("angularVelocity", Severity.Ignore)) realAngularVelocity = value * timeline.timeScale; }
  220. }
  221. /// <summary>
  222. /// The equivalent of Rigidbody.AddForce adjusted for time effects.
  223. /// </summary>
  224. public void AddForce(Vector3 force, ForceMode mode = ForceMode.Force)
  225. {
  226. if (AssertForwardForce(Severity.Ignore))
  227. component.AddForce(AdjustForce(force), mode);
  228. }
  229. /// <summary>
  230. /// The equivalent of Rigidbody.AddRelativeForce adjusted for time effects.
  231. /// </summary>
  232. public void AddRelativeForce(Vector3 force, ForceMode mode = ForceMode.Force)
  233. {
  234. if (AssertForwardForce(Severity.Ignore))
  235. component.AddRelativeForce(AdjustForce(force), mode);
  236. }
  237. /// <summary>
  238. /// The equivalent of Rigidbody.AddForceAtPosition adjusted for time effects.
  239. /// </summary>
  240. public void AddForceAtPosition(Vector3 force, Vector3 position, ForceMode mode = ForceMode.Force)
  241. {
  242. if (AssertForwardForce(Severity.Ignore))
  243. component.AddForceAtPosition(AdjustForce(force), position, mode);
  244. }
  245. /// <summary>
  246. /// The equivalent of Rigidbody.AddRelativeForce adjusted for time effects.
  247. /// </summary>
  248. public void AddExplosionForce(float explosionForce, Vector3 explosionPosition, float explosionRadius,
  249. float upwardsModifier = 0, ForceMode mode = ForceMode.Force)
  250. {
  251. if (AssertForwardForce(Severity.Ignore))
  252. component.AddExplosionForce(AdjustForce(explosionForce), explosionPosition, explosionRadius, upwardsModifier, mode);
  253. }
  254. /// <summary>
  255. /// The equivalent of Rigidbody.AddTorque adjusted for time effects.
  256. /// </summary>
  257. public void AddTorque(Vector3 torque, ForceMode mode = ForceMode.Force)
  258. {
  259. if (AssertForwardForce(Severity.Ignore))
  260. component.AddTorque(AdjustForce(torque), mode);
  261. }
  262. /// <summary>
  263. /// The equivalent of Rigidbody.AddRelativeTorque adjusted for time effects.
  264. /// </summary>
  265. public void AddRelativeTorque(Vector3 torque, ForceMode mode = ForceMode.Force)
  266. {
  267. if (AssertForwardForce(Severity.Ignore))
  268. component.AddRelativeTorque(AdjustForce(torque), mode);
  269. }
  270. #endregion
  271. }
  272. }