// Author: Daniele Giardini - http://www.demigiant.com // Created: 2018/07/13 #if false // MODULE_MARKER using System; using DG.Tweening.Core; using DG.Tweening.Plugins; using DG.Tweening.Plugins.Core.PathCore; using DG.Tweening.Plugins.Options; using UnityEngine; #pragma warning disable 1591 namespace DG.Tweening { public static class DOTweenModulePhysics2D { #region Shortcuts #region Rigidbody2D Shortcuts /// Tweens a Rigidbody2D's position to the given value. /// Also stores the Rigidbody2D as the tween's target so it can be used for filtered operations /// The end value to reachThe duration of the tween /// If TRUE the tween will smoothly snap all values to integers public static TweenerCore DOMove(this Rigidbody2D target, Vector2 endValue, float duration, bool snapping = false) { TweenerCore t = DOTween.To(() => target.position, target.MovePosition, endValue, duration); t.SetOptions(snapping).SetTarget(target); return t; } /// Tweens a Rigidbody2D's X position to the given value. /// Also stores the Rigidbody2D as the tween's target so it can be used for filtered operations /// The end value to reachThe duration of the tween /// If TRUE the tween will smoothly snap all values to integers public static TweenerCore DOMoveX(this Rigidbody2D target, float endValue, float duration, bool snapping = false) { TweenerCore t = DOTween.To(() => target.position, target.MovePosition, new Vector2(endValue, 0), duration); t.SetOptions(AxisConstraint.X, snapping).SetTarget(target); return t; } /// Tweens a Rigidbody2D's Y position to the given value. /// Also stores the Rigidbody2D as the tween's target so it can be used for filtered operations /// The end value to reachThe duration of the tween /// If TRUE the tween will smoothly snap all values to integers public static TweenerCore DOMoveY(this Rigidbody2D target, float endValue, float duration, bool snapping = false) { TweenerCore t = DOTween.To(() => target.position, target.MovePosition, new Vector2(0, endValue), duration); t.SetOptions(AxisConstraint.Y, snapping).SetTarget(target); return t; } /// Tweens a Rigidbody2D's rotation to the given value. /// Also stores the Rigidbody2D as the tween's target so it can be used for filtered operations /// The end value to reachThe duration of the tween public static TweenerCore DORotate(this Rigidbody2D target, float endValue, float duration) { TweenerCore t = DOTween.To(() => target.rotation, target.MoveRotation, endValue, duration); t.SetTarget(target); return t; } #region Special /// Tweens a Rigidbody2D's position to the given value, while also applying a jump effect along the Y axis. /// Returns a Sequence instead of a Tweener. /// Also stores the Rigidbody2D as the tween's target so it can be used for filtered operations. /// IMPORTANT: a rigidbody2D can't be animated in a jump arc using MovePosition, so the tween will directly set the position /// The end value to reach /// Power of the jump (the max height of the jump is represented by this plus the final Y offset) /// Total number of jumps /// The duration of the tween /// If TRUE the tween will smoothly snap all values to integers public static Sequence DOJump(this Rigidbody2D target, Vector2 endValue, float jumpPower, int numJumps, float duration, bool snapping = false) { if (numJumps < 1) numJumps = 1; float startPosY = 0; float offsetY = -1; bool offsetYSet = false; Sequence s = DOTween.Sequence(); Tween yTween = DOTween.To(() => target.position, x => target.position = x, new Vector2(0, jumpPower), duration / (numJumps * 2)) .SetOptions(AxisConstraint.Y, snapping).SetEase(Ease.OutQuad).SetRelative() .SetLoops(numJumps * 2, LoopType.Yoyo) .OnStart(() => startPosY = target.position.y); s.Append(DOTween.To(() => target.position, x => target.position = x, new Vector2(endValue.x, 0), duration) .SetOptions(AxisConstraint.X, snapping).SetEase(Ease.Linear) ).Join(yTween) .SetTarget(target).SetEase(DOTween.defaultEaseType); yTween.OnUpdate(() => { if (!offsetYSet) { offsetYSet = true; offsetY = s.isRelative ? endValue.y : endValue.y - startPosY; } Vector3 pos = target.position; pos.y += DOVirtual.EasedValue(0, offsetY, yTween.ElapsedPercentage(), Ease.OutQuad); target.MovePosition(pos); }); return s; } /// Tweens a Rigidbody2D's position through the given path waypoints, using the chosen path algorithm. /// Also stores the Rigidbody2D as the tween's target so it can be used for filtered operations. /// NOTE: to tween a Rigidbody2D correctly it should be set to kinematic at least while being tweened. /// BEWARE: doesn't work on Windows Phone store (waiting for Unity to fix their own bug). /// If you plan to publish there you should use a regular transform.DOPath. /// The waypoints to go through /// The duration of the tween /// The type of path: Linear (straight path), CatmullRom (curved CatmullRom path) or CubicBezier (curved with control points) /// The path mode: 3D, side-scroller 2D, top-down 2D /// The resolution of the path (useless in case of Linear paths): higher resolutions make for more detailed curved paths but are more expensive. /// Defaults to 10, but a value of 5 is usually enough if you don't have dramatic long curves between waypoints /// The color of the path (shown when gizmos are active in the Play panel and the tween is running) public static TweenerCore DOPath( this Rigidbody2D target, Vector2[] path, float duration, PathType pathType = PathType.Linear, PathMode pathMode = PathMode.Full3D, int resolution = 10, Color? gizmoColor = null ) { if (resolution < 1) resolution = 1; int len = path.Length; Vector3[] path3D = new Vector3[len]; for (int i = 0; i < len; ++i) path3D[i] = path[i]; TweenerCore t = DOTween.To(PathPlugin.Get(), () => target.position, x => target.MovePosition(x), new Path(pathType, path3D, resolution, gizmoColor), duration) .SetTarget(target).SetUpdate(UpdateType.Fixed); t.plugOptions.isRigidbody2D = true; t.plugOptions.mode = pathMode; return t; } /// Tweens a Rigidbody2D's localPosition through the given path waypoints, using the chosen path algorithm. /// Also stores the Rigidbody2D as the tween's target so it can be used for filtered operations /// NOTE: to tween a Rigidbody2D correctly it should be set to kinematic at least while being tweened. /// BEWARE: doesn't work on Windows Phone store (waiting for Unity to fix their own bug). /// If you plan to publish there you should use a regular transform.DOLocalPath. /// The waypoint to go through /// The duration of the tween /// The type of path: Linear (straight path), CatmullRom (curved CatmullRom path) or CubicBezier (curved with control points) /// The path mode: 3D, side-scroller 2D, top-down 2D /// The resolution of the path: higher resolutions make for more detailed curved paths but are more expensive. /// Defaults to 10, but a value of 5 is usually enough if you don't have dramatic long curves between waypoints /// The color of the path (shown when gizmos are active in the Play panel and the tween is running) public static TweenerCore DOLocalPath( this Rigidbody2D target, Vector2[] path, float duration, PathType pathType = PathType.Linear, PathMode pathMode = PathMode.Full3D, int resolution = 10, Color? gizmoColor = null ) { if (resolution < 1) resolution = 1; int len = path.Length; Vector3[] path3D = new Vector3[len]; for (int i = 0; i < len; ++i) path3D[i] = path[i]; Transform trans = target.transform; TweenerCore 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) .SetTarget(target).SetUpdate(UpdateType.Fixed); t.plugOptions.isRigidbody2D = true; t.plugOptions.mode = pathMode; t.plugOptions.useLocalPosition = true; return t; } // Used by path editor when creating the actual tween, so it can pass a pre-compiled path internal static TweenerCore DOPath( this Rigidbody2D target, Path path, float duration, PathMode pathMode = PathMode.Full3D ) { TweenerCore t = DOTween.To(PathPlugin.Get(), () => target.position, x => target.MovePosition(x), path, duration) .SetTarget(target); t.plugOptions.isRigidbody2D = true; t.plugOptions.mode = pathMode; return t; } internal static TweenerCore DOLocalPath( this Rigidbody2D target, Path path, float duration, PathMode pathMode = PathMode.Full3D ) { Transform trans = target.transform; TweenerCore t = DOTween.To(PathPlugin.Get(), () => trans.localPosition, x => target.MovePosition(trans.parent == null ? x : trans.parent.TransformPoint(x)), path, duration) .SetTarget(target); t.plugOptions.isRigidbody2D = true; t.plugOptions.mode = pathMode; t.plugOptions.useLocalPosition = true; return t; } #endregion #endregion #endregion } } #endif