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