DOTweenModulePhysics2D.cs 12 KB

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