public static Sequence DOJumpAnchorPos(this RectTransform target, Vector2 endValue, float jumpPower, int numJumps, float duration, bool snapping = false)
        {
            if (numJumps < 1)
            {
                numJumps = 1;
            }
            float    startPosY  = 0f;
            float    offsetY    = -1f;
            bool     offsetYSet = false;
            Sequence s          = DOTween.Sequence();
            Tween    t          = DOTween.To(() => target.anchoredPosition, delegate(Vector2 x)
            {
                target.anchoredPosition = x;
            }, new Vector2(0f, jumpPower), duration / (float)(numJumps * 2)).SetOptions(AxisConstraint.Y, snapping).SetEase(Ease.OutQuad)
                                  .SetRelative()
                                  .SetLoops(numJumps * 2, LoopType.Yoyo)
                                  .OnStart(delegate
            {
                Vector2 anchoredPosition2 = target.anchoredPosition;
                startPosY = anchoredPosition2.y;
            });

            s.Append(DOTween.To(() => target.anchoredPosition, delegate(Vector2 x)
            {
                target.anchoredPosition = x;
            }, new Vector2(endValue.x, 0f), duration).SetOptions(AxisConstraint.X, snapping).SetEase(Ease.Linear)).Join(t).SetTarget(target)
            .SetEase(DOTween.defaultEaseType);
            s.OnUpdate(delegate
            {
                if (!offsetYSet)
                {
                    offsetYSet = true;
                    offsetY    = ((!s.isRelative) ? (endValue.y - startPosY) : endValue.y);
                }
                Vector2 anchoredPosition = target.anchoredPosition;
                anchoredPosition.y      += DOVirtual.EasedValue(0f, offsetY, s.ElapsedDirectionalPercentage(), Ease.OutQuad);
                target.anchoredPosition  = anchoredPosition;
            });
            return(s);
        }
        /// <summary>Tweens a RectTransform's anchoredPosition to the given value, while also applying a jump effect along the Y axis.
        /// Returns a Sequence instead of a Tweener.
        /// Also stores the RectTransform as the tween's target so it can be used for filtered operations</summary>
        /// <param name="endValue">The end value to reach</param>
        /// <param name="jumpPower">Power of the jump (the max height of the jump is represented by this plus the final Y offset)</param>
        /// <param name="numJumps">Total number of jumps</param>
        /// <param name="duration">The duration of the tween</param>
        /// <param name="snapping">If TRUE the tween will smoothly snap all values to integers</param>
        public static Sequence DOJumpAnchorPos(this RectTransform 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;

            // Separate Y Tween so we can elaborate elapsedPercentage on that insted of on the Sequence
            // (in case users add a delay or other elements to the Sequence)
            Sequence s      = DOTween.Sequence();
            Tween    yTween = DOTween.To(() => target.anchoredPosition, x => target.anchoredPosition = x,
                                         new Vector2(0, jumpPower), duration / (numJumps * 2))
                              .SetOptions(AxisConstraint.Y, snapping).SetEase(Ease.OutQuad).SetRelative()
                              .SetLoops(numJumps * 2, LoopType.Yoyo)
                              .OnStart(() => startPosY = target.anchoredPosition.y);

            s.Append(DOTween.To(() => target.anchoredPosition, x => target.anchoredPosition = x, new Vector2(endValue.x, 0),
                                duration)
                     .SetOptions(AxisConstraint.X, snapping).SetEase(Ease.Linear)
                     ).Join(yTween)
            .SetTarget(target).SetEase(DOTween.defaultEaseType);
            s.OnUpdate(() =>
            {
                if (!offsetYSet)
                {
                    offsetYSet = true;
                    offsetY    = s.isRelative ? endValue.y : endValue.y - startPosY;
                }

                Vector2 pos             = target.anchoredPosition;
                pos.y                  += DOVirtual.EasedValue(0, offsetY, s.ElapsedDirectionalPercentage(), Ease.OutQuad);
                target.anchoredPosition = pos;
            });
            return(s);
        }
        /// <summary>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.
        /// <para>IMPORTANT: a rigidbody2D can't be animated in a jump arc using MovePosition, so the tween will directly set the position</para></summary>
        /// <param name="endValue">The end value to reach</param>
        /// <param name="jumpPower">Power of the jump (the max height of the jump is represented by this plus the final Y offset)</param>
        /// <param name="numJumps">Total number of jumps</param>
        /// <param name="duration">The duration of the tween</param>
        /// <param name="snapping">If TRUE the tween will smoothly snap all values to integers</param>
        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();

#if COMPATIBLE
            Tween yTween = DOTween.To(() => target.position, x => target.position = x.value, 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.value, new Vector2(endValue.x, 0), duration)
#else
            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)
#endif
                     .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);
        }
Exemple #4
0
        public static Sequence DOLocalJump(this Transform target, Vector3 endValue, float jumpPower, int numJumps, float duration, bool snapping = false)
        {
            if (numJumps < 1)
            {
                numJumps = 1;
            }
            float    startPosY  = target.localPosition.y;
            float    offsetY    = -1f;
            bool     offsetYSet = false;
            Sequence s          = DOTween.Sequence();

            s.Append(DOTween.To(() => target.localPosition, delegate(Vector3 x)
            {
                target.localPosition = x;
            }, new Vector3(endValue.x, 0f, 0f), duration).SetOptions(AxisConstraint.X, snapping).SetEase(Ease.Linear)
                     .OnUpdate(delegate
            {
                if (!offsetYSet)
                {
                    offsetYSet = false;
                    offsetY    = (s.isRelative ? endValue.y : (endValue.y - startPosY));
                }
                Vector3 localPosition = target.localPosition;
                localPosition.y      += DOVirtual.EasedValue(0f, offsetY, s.ElapsedDirectionalPercentage(), Ease.OutQuad);
                target.localPosition  = localPosition;
            })).Join(DOTween.To(() => target.localPosition, delegate(Vector3 x)
            {
                target.localPosition = x;
            }, new Vector3(0f, 0f, endValue.z), duration).SetOptions(AxisConstraint.Z, snapping).SetEase(Ease.Linear)).Join(DOTween.To(() => target.localPosition, delegate(Vector3 x)
            {
                target.localPosition = x;
            }, new Vector3(0f, jumpPower, 0f), duration / (float)(numJumps * 2)).SetOptions(AxisConstraint.Y, snapping).SetEase(Ease.OutQuad)
                                                                                                                            .SetRelative()
                                                                                                                            .SetLoops(numJumps * 2, LoopType.Yoyo))
            .SetTarget(target)
            .SetEase(DOTween.defaultEaseType);
            return(s);
        }
        public static Sequence DOJump(this Rigidbody target, Vector3 endValue, float jumpPower, int numJumps, float duration, bool snapping = false)
        {
            if (numJumps < 1)
            {
                numJumps = 1;
            }
            float    startPosY  = target.position.y;
            float    offsetY    = -1f;
            bool     offsetYSet = false;
            Sequence s          = DOTween.Sequence();

            s.Append(DOTween.To(() => target.position, new DOSetter <Vector3>(target.MovePosition), new Vector3(endValue.x, 0f, 0f), duration).SetOptions(AxisConstraint.X, snapping).SetEase <Tweener>(Ease.Linear).OnUpdate <Tweener>(delegate {
                if (!offsetYSet)
                {
                    offsetYSet = true;
                    offsetY    = s.isRelative ? endValue.y : (endValue.y - startPosY);
                }
                Vector3 position = target.position;
                position.y      += DOVirtual.EasedValue(0f, offsetY, s.ElapsedDirectionalPercentage(), Ease.OutQuad);
                target.MovePosition(position);
            })).Join(DOTween.To(() => target.position, new DOSetter <Vector3>(target.MovePosition), new Vector3(0f, 0f, endValue.z), duration).SetOptions(AxisConstraint.Z, snapping).SetEase <Tweener>(Ease.Linear)).Join(DOTween.To(() => target.position, new DOSetter <Vector3>(target.MovePosition), new Vector3(0f, jumpPower, 0f), duration / ((float)(numJumps * 2))).SetOptions(AxisConstraint.Y, snapping).SetEase <Tweener>(Ease.OutQuad).SetLoops <Tweener>((numJumps * 2), LoopType.Yoyo).SetRelative <Tweener>()).SetTarget <Sequence>(target).SetEase <Sequence>(DOTween.defaultEaseType);
            return(s);
        }