DOTweenModulePhysics.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. // Author: Daniele Giardini - http://www.demigiant.com
  2. // Created: 2018/07/13
  3. #if true // MODULE_MARKER
  4. using System;
  5. using DG.Tweening.Core;
  6. using DG.Tweening.Core.Enums;
  7. using DG.Tweening.Plugins;
  8. using DG.Tweening.Plugins.Core.PathCore;
  9. using DG.Tweening.Plugins.Options;
  10. using UnityEngine;
  11. #pragma warning disable 1591
  12. namespace DG.Tweening
  13. {
  14. public static class DOTweenModulePhysics
  15. {
  16. #region Shortcuts
  17. #region Rigidbody
  18. /// <summary>Tweens a Rigidbody's position to the given value.
  19. /// Also stores the rigidbody as the tween's target so it can be used for filtered operations</summary>
  20. /// <param name="endValue">The end value to reach</param><param name="duration">The duration of the tween</param>
  21. /// <param name="snapping">If TRUE the tween will smoothly snap all values to integers</param>
  22. public static TweenerCore<Vector3, Vector3, VectorOptions> DOMove(this Rigidbody target, Vector3 endValue, float duration, bool snapping = false)
  23. {
  24. TweenerCore<Vector3, Vector3, VectorOptions> t = DOTween.To(() => target.position, target.MovePosition, endValue, duration);
  25. t.SetOptions(snapping).SetTarget(target);
  26. return t;
  27. }
  28. /// <summary>Tweens a Rigidbody's X position to the given value.
  29. /// Also stores the rigidbody as the tween's target so it can be used for filtered operations</summary>
  30. /// <param name="endValue">The end value to reach</param><param name="duration">The duration of the tween</param>
  31. /// <param name="snapping">If TRUE the tween will smoothly snap all values to integers</param>
  32. public static TweenerCore<Vector3, Vector3, VectorOptions> DOMoveX(this Rigidbody target, float endValue, float duration, bool snapping = false)
  33. {
  34. TweenerCore<Vector3, Vector3, VectorOptions> t = DOTween.To(() => target.position, target.MovePosition, new Vector3(endValue, 0, 0), duration);
  35. t.SetOptions(AxisConstraint.X, snapping).SetTarget(target);
  36. return t;
  37. }
  38. /// <summary>Tweens a Rigidbody's Y position to the given value.
  39. /// Also stores the rigidbody as the tween's target so it can be used for filtered operations</summary>
  40. /// <param name="endValue">The end value to reach</param><param name="duration">The duration of the tween</param>
  41. /// <param name="snapping">If TRUE the tween will smoothly snap all values to integers</param>
  42. public static TweenerCore<Vector3, Vector3, VectorOptions> DOMoveY(this Rigidbody target, float endValue, float duration, bool snapping = false)
  43. {
  44. TweenerCore<Vector3, Vector3, VectorOptions> t = DOTween.To(() => target.position, target.MovePosition, new Vector3(0, endValue, 0), duration);
  45. t.SetOptions(AxisConstraint.Y, snapping).SetTarget(target);
  46. return t;
  47. }
  48. /// <summary>Tweens a Rigidbody's Z position to the given value.
  49. /// Also stores the rigidbody as the tween's target so it can be used for filtered operations</summary>
  50. /// <param name="endValue">The end value to reach</param><param name="duration">The duration of the tween</param>
  51. /// <param name="snapping">If TRUE the tween will smoothly snap all values to integers</param>
  52. public static TweenerCore<Vector3, Vector3, VectorOptions> DOMoveZ(this Rigidbody target, float endValue, float duration, bool snapping = false)
  53. {
  54. TweenerCore<Vector3, Vector3, VectorOptions> t = DOTween.To(() => target.position, target.MovePosition, new Vector3(0, 0, endValue), duration);
  55. t.SetOptions(AxisConstraint.Z, snapping).SetTarget(target);
  56. return t;
  57. }
  58. /// <summary>Tweens a Rigidbody's rotation to the given value.
  59. /// Also stores the rigidbody as the tween's target so it can be used for filtered operations</summary>
  60. /// <param name="endValue">The end value to reach</param><param name="duration">The duration of the tween</param>
  61. /// <param name="mode">Rotation mode</param>
  62. public static TweenerCore<Quaternion, Vector3, QuaternionOptions> DORotate(this Rigidbody target, Vector3 endValue, float duration, RotateMode mode = RotateMode.Fast)
  63. {
  64. TweenerCore<Quaternion, Vector3, QuaternionOptions> t = DOTween.To(() => target.rotation, target.MoveRotation, endValue, duration);
  65. t.SetTarget(target);
  66. t.plugOptions.rotateMode = mode;
  67. return t;
  68. }
  69. /// <summary>Tweens a Rigidbody's rotation so that it will look towards the given position.
  70. /// Also stores the rigidbody as the tween's target so it can be used for filtered operations</summary>
  71. /// <param name="towards">The position to look at</param><param name="duration">The duration of the tween</param>
  72. /// <param name="axisConstraint">Eventual axis constraint for the rotation</param>
  73. /// <param name="up">The vector that defines in which direction up is (default: Vector3.up)</param>
  74. public static TweenerCore<Quaternion, Vector3, QuaternionOptions> DOLookAt(this Rigidbody target, Vector3 towards, float duration, AxisConstraint axisConstraint = AxisConstraint.None, Vector3? up = null)
  75. {
  76. TweenerCore<Quaternion, Vector3, QuaternionOptions> t = DOTween.To(() => target.rotation, target.MoveRotation, towards, duration)
  77. .SetTarget(target).SetSpecialStartupMode(SpecialStartupMode.SetLookAt);
  78. t.plugOptions.axisConstraint = axisConstraint;
  79. t.plugOptions.up = (up == null) ? Vector3.up : (Vector3)up;
  80. return t;
  81. }
  82. #region Special
  83. /// <summary>Tweens a Rigidbody's position to the given value, while also applying a jump effect along the Y axis.
  84. /// Returns a Sequence instead of a Tweener.
  85. /// Also stores the Rigidbody as the tween's target so it can be used for filtered operations</summary>
  86. /// <param name="endValue">The end value to reach</param>
  87. /// <param name="jumpPower">Power of the jump (the max height of the jump is represented by this plus the final Y offset)</param>
  88. /// <param name="numJumps">Total number of jumps</param>
  89. /// <param name="duration">The duration of the tween</param>
  90. /// <param name="snapping">If TRUE the tween will smoothly snap all values to integers</param>
  91. public static Sequence DOJump(this Rigidbody target, Vector3 endValue, float jumpPower, int numJumps, float duration, bool snapping = false)
  92. {
  93. if (numJumps < 1) numJumps = 1;
  94. float startPosY = 0;
  95. float offsetY = -1;
  96. bool offsetYSet = false;
  97. Sequence s = DOTween.Sequence();
  98. Tween yTween = DOTween.To(() => target.position, target.MovePosition, new Vector3(0, jumpPower, 0), duration / (numJumps * 2))
  99. .SetOptions(AxisConstraint.Y, snapping).SetEase(Ease.OutQuad).SetRelative()
  100. .SetLoops(numJumps * 2, LoopType.Yoyo)
  101. .OnStart(() => startPosY = target.position.y);
  102. s.Append(DOTween.To(() => target.position, target.MovePosition, new Vector3(endValue.x, 0, 0), duration)
  103. .SetOptions(AxisConstraint.X, snapping).SetEase(Ease.Linear)
  104. ).Join(DOTween.To(() => target.position, target.MovePosition, new Vector3(0, 0, endValue.z), duration)
  105. .SetOptions(AxisConstraint.Z, snapping).SetEase(Ease.Linear)
  106. ).Join(yTween)
  107. .SetTarget(target).SetEase(DOTween.defaultEaseType);
  108. yTween.OnUpdate(() => {
  109. if (!offsetYSet) {
  110. offsetYSet = true;
  111. offsetY = s.isRelative ? endValue.y : endValue.y - startPosY;
  112. }
  113. Vector3 pos = target.position;
  114. pos.y += DOVirtual.EasedValue(0, offsetY, yTween.ElapsedPercentage(), Ease.OutQuad);
  115. target.MovePosition(pos);
  116. });
  117. return s;
  118. }
  119. /// <summary>Tweens a Rigidbody's position through the given path waypoints, using the chosen path algorithm.
  120. /// Also stores the Rigidbody as the tween's target so it can be used for filtered operations.
  121. /// <para>NOTE: to tween a rigidbody correctly it should be set to kinematic at least while being tweened.</para>
  122. /// <para>BEWARE: doesn't work on Windows Phone store (waiting for Unity to fix their own bug).
  123. /// If you plan to publish there you should use a regular transform.DOPath.</para></summary>
  124. /// <param name="path">The waypoints to go through</param>
  125. /// <param name="duration">The duration of the tween</param>
  126. /// <param name="pathType">The type of path: Linear (straight path), CatmullRom (curved CatmullRom path) or CubicBezier (curved with control points)</param>
  127. /// <param name="pathMode">The path mode: 3D, side-scroller 2D, top-down 2D</param>
  128. /// <param name="resolution">The resolution of the path (useless in case of Linear paths): higher resolutions make for more detailed curved paths but are more expensive.
  129. /// Defaults to 10, but a value of 5 is usually enough if you don't have dramatic long curves between waypoints</param>
  130. /// <param name="gizmoColor">The color of the path (shown when gizmos are active in the Play panel and the tween is running)</param>
  131. public static TweenerCore<Vector3, Path, PathOptions> DOPath(
  132. this Rigidbody target, Vector3[] path, float duration, PathType pathType = PathType.Linear,
  133. PathMode pathMode = PathMode.Full3D, int resolution = 10, Color? gizmoColor = null
  134. )
  135. {
  136. if (resolution < 1) resolution = 1;
  137. TweenerCore<Vector3, Path, PathOptions> t = DOTween.To(PathPlugin.Get(), () => target.position, target.MovePosition, new Path(pathType, path, resolution, gizmoColor), duration)
  138. .SetTarget(target).SetUpdate(UpdateType.Fixed);
  139. t.plugOptions.isRigidbody = true;
  140. t.plugOptions.mode = pathMode;
  141. return t;
  142. }
  143. /// <summary>Tweens a Rigidbody's localPosition through the given path waypoints, using the chosen path algorithm.
  144. /// Also stores the Rigidbody as the tween's target so it can be used for filtered operations
  145. /// <para>NOTE: to tween a rigidbody correctly it should be set to kinematic at least while being tweened.</para>
  146. /// <para>BEWARE: doesn't work on Windows Phone store (waiting for Unity to fix their own bug).
  147. /// If you plan to publish there you should use a regular transform.DOLocalPath.</para></summary>
  148. /// <param name="path">The waypoint to go through</param>
  149. /// <param name="duration">The duration of the tween</param>
  150. /// <param name="pathType">The type of path: Linear (straight path), CatmullRom (curved CatmullRom path) or CubicBezier (curved with control points)</param>
  151. /// <param name="pathMode">The path mode: 3D, side-scroller 2D, top-down 2D</param>
  152. /// <param name="resolution">The resolution of the path: higher resolutions make for more detailed curved paths but are more expensive.
  153. /// Defaults to 10, but a value of 5 is usually enough if you don't have dramatic long curves between waypoints</param>
  154. /// <param name="gizmoColor">The color of the path (shown when gizmos are active in the Play panel and the tween is running)</param>
  155. public static TweenerCore<Vector3, Path, PathOptions> DOLocalPath(
  156. this Rigidbody target, Vector3[] path, float duration, PathType pathType = PathType.Linear,
  157. PathMode pathMode = PathMode.Full3D, int resolution = 10, Color? gizmoColor = null
  158. )
  159. {
  160. if (resolution < 1) resolution = 1;
  161. Transform trans = target.transform;
  162. TweenerCore<Vector3, Path, PathOptions> t = DOTween.To(PathPlugin.Get(), () => trans.localPosition, x => target.MovePosition(trans.parent == null ? x : trans.parent.TransformPoint(x)), new Path(pathType, path, resolution, gizmoColor), duration)
  163. .SetTarget(target).SetUpdate(UpdateType.Fixed);
  164. t.plugOptions.isRigidbody = true;
  165. t.plugOptions.mode = pathMode;
  166. t.plugOptions.useLocalPosition = true;
  167. return t;
  168. }
  169. // Used by path editor when creating the actual tween, so it can pass a pre-compiled path
  170. internal static TweenerCore<Vector3, Path, PathOptions> DOPath(
  171. this Rigidbody target, Path path, float duration, PathMode pathMode = PathMode.Full3D
  172. )
  173. {
  174. TweenerCore<Vector3, Path, PathOptions> t = DOTween.To(PathPlugin.Get(), () => target.position, target.MovePosition, path, duration)
  175. .SetTarget(target);
  176. t.plugOptions.isRigidbody = true;
  177. t.plugOptions.mode = pathMode;
  178. return t;
  179. }
  180. internal static TweenerCore<Vector3, Path, PathOptions> DOLocalPath(
  181. this Rigidbody target, Path path, float duration, PathMode pathMode = PathMode.Full3D
  182. )
  183. {
  184. Transform trans = target.transform;
  185. TweenerCore<Vector3, Path, PathOptions> t = DOTween.To(PathPlugin.Get(), () => trans.localPosition, x => target.MovePosition(trans.parent == null ? x : trans.parent.TransformPoint(x)), path, duration)
  186. .SetTarget(target);
  187. t.plugOptions.isRigidbody = true;
  188. t.plugOptions.mode = pathMode;
  189. t.plugOptions.useLocalPosition = true;
  190. return t;
  191. }
  192. #endregion
  193. #endregion
  194. #endregion
  195. }
  196. }
  197. #endif