/// <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();
            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);
        }
Exemple #2
0
        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  = 0f;
            float    offsetY    = -1f;
            bool     offsetYSet = false;
            Sequence s          = DOTween.Sequence();
            Tween    yTween     = DOTween.To(() => target.position, target.MovePosition, new Vector3(0f, jumpPower, 0f), duration / (float)(numJumps * 2)).SetOptions(AxisConstraint.Y, snapping).SetEase(Ease.OutQuad)
                                  .SetRelative()
                                  .SetLoops(numJumps * 2, LoopType.Yoyo)
                                  .OnStart(delegate
            {
                Vector3 position2 = target.position;
                startPosY         = position2.y;
            });

            s.Append(DOTween.To(() => target.position, target.MovePosition, new Vector3(endValue.x, 0f, 0f), duration).SetOptions(AxisConstraint.X, snapping).SetEase(Ease.Linear)).Join(DOTween.To(() => target.position, target.MovePosition, new Vector3(0f, 0f, endValue.z), duration).SetOptions(AxisConstraint.Z, snapping).SetEase(Ease.Linear)).Join(yTween)
            .SetTarget(target)
            .SetEase(DOTween.defaultEaseType);
            yTween.OnUpdate(delegate
            {
                if (!offsetYSet)
                {
                    offsetYSet = true;
                    offsetY    = ((!s.isRelative) ? (endValue.y - startPosY) : endValue.y);
                }
                Vector3 position = target.position;
                position.y      += DOVirtual.EasedValue(0f, offsetY, yTween.ElapsedPercentage(), Ease.OutQuad);
                target.MovePosition(position);
            });
            return(s);
        }
 static int OnUpdate(IntPtr L)
 {
     try
     {
         ToLua.CheckArgsCount(L, 2);
         DG.Tweening.Tween         obj  = (DG.Tweening.Tween)ToLua.CheckObject <DG.Tweening.Tween>(L, 1);
         DG.Tweening.TweenCallback arg0 = (DG.Tweening.TweenCallback)ToLua.CheckDelegate <DG.Tweening.TweenCallback>(L, 2);
         DG.Tweening.Tween         o    = obj.OnUpdate(arg0);
         ToLua.PushObject(L, o);
         return(1);
     }
     catch (Exception e)
     {
         return(LuaDLL.toluaL_exception(L, e));
     }
 }
    static int OnUpdate(IntPtr L)
    {
        LuaScriptMgr.CheckArgsCount(L, 2);
        DG.Tweening.Tween         obj  = (DG.Tweening.Tween)LuaScriptMgr.GetNetObjectSelf(L, 1, "DG.Tweening.Tween");
        DG.Tweening.TweenCallback arg0 = null;
        LuaTypes funcType2             = LuaDLL.lua_type(L, 2);

        if (funcType2 != LuaTypes.LUA_TFUNCTION)
        {
            arg0 = (DG.Tweening.TweenCallback)LuaScriptMgr.GetNetObject(L, 2, typeof(DG.Tweening.TweenCallback));
        }
        else
        {
            LuaFunction func = LuaScriptMgr.GetLuaFunction(L, 2);
            arg0 = () =>
            {
                func.Call();
            };
        }

        DG.Tweening.Tween o = obj.OnUpdate(arg0);
        LuaScriptMgr.PushObject(L, o);
        return(1);
    }