Demigiant / dotween

A Unity C# animation engine. HOTween v2
http://dotween.demigiant.com
Other
2.34k stars 351 forks source link

Create custom tween methods advanced (directional DOJump) #642

Open Tehenauin opened 1 year ago

Tehenauin commented 1 year ago

Hi, I am not sure, if this exists in some kind, but I would really like to do my own interpolation calculations. There is the DoTween.To(...) Method. It is great, but unfortunately it only has a getter, that defines where to get the start value from and the setter, that lets you make use of the tweened value. In addition I would love to have something that lets me take control of how the tweened value gets calculated by getting time value between 0 and 1 and letting me do stuff with it.

In my case I want to make a jump tween that lets my define a custom up axis.

Is there maybe something like this already, that I am just missing by chance?

Tehenauin commented 1 year ago

Not the most intuitive, but I found a way to do it. I can simply tween between 0 and 1 values. Then I can use the tweened value as time. Sorry for bothering you.

    public static Tween DOVectorJump(this Transform transform, Vector3 endValue, Vector3 upVector, float jumpheight, float duration){

        Vector3 from = transform.position;
        return DOTween.To(
            () => 0f,
            t => transform.position = CalcJumpVector(from, endValue, t, upVector, jumpheight),
            1f, duration
            );
    }

    private static Vector3 CalcJumpVector(Vector3 from, Vector3 to, float time, Vector3 upVector, float jumpheight)
    {
        float jumpVal = 4 * time - 4 * time * time;
        Vector3 moveVec = Vector3.LerpUnclamped(from, to, time);
        return moveVec + jumpVal * jumpheight * upVector.normalized;
    }
Demigiant commented 1 year ago

No bother, I applaud this! I'm sueprbusy with some other DOTween stuff but later I'll see if I can integrate this in the source (and credit you accordingly) :)