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