Demigiant / dotween

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

Can you use DoJump along with x,z axis #621

Open KeyboardRules opened 1 year ago

KeyboardRules commented 1 year ago

I'm working in a top down 2.5D games and i'm using x,y axis as forward and left movement, and i want to jump along with z axis, so i wonder is there's a way to change jump from Y axis to Z axis?

Tehenauin commented 1 year ago

I am looking for something similar. In my case it would be great if I could define a custom up vector.

Tehenauin commented 1 year ago

In case somebody stumbles upon this. Here is my solution:

    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;
    }

This doesn't include all the functionality, that the standard DOTween Jump has, but it worked for me. Maybe it can help you too.

utsavll0 commented 1 year ago

Hey @Tehenauin I cant thank you enough. We have been stuck on this issue for so long, I came across your answer and it works like a charm

Can you briefly explain how does the CalcJumpVector work? And how does that fit in the DoTween To function?

BimBamBum-Games commented 11 months ago

In case somebody stumbles upon this. Here is my solution:

    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;
    }

This doesn't include all the functionality, that the standard DOTween Jump has, but it worked for me. Maybe it can help you too.

this works well, i changed it a little bit to local variation :D

Tehenauin commented 11 months ago

Hey @Tehenauin I cant thank you enough. We have been stuck on this issue for so long, I came across your answer and it works like a charm

Can you briefly explain how does the CalcJumpVector work? And how does that fit in the DoTween To function?

I am very sorry, for the late reply. unfortunately I have seen your comment just now.

The CalcJumpVector method interpolates a vector between the from and to Vectors just as it does in DOMove but it adds the jump vector with a multiplication dependent on the time.

You can read jumpVal = 4 * time - 4 * time * time; like this: y = 4x - 4x² its just a quatratic function that has a peak of 1 at 0.5 and is 0 when x is 0 or 1. Thats how it looks

The DoTween.To function is just the generic tweening funcion I have set the getter to () => 0f and the to value to 1 so it just interpolates between 0 and 1, and I can use the outcome as time for my CalcJumpVector Method.

I hope it is understandable. I am bad in explaining.

BimBamBum-Games commented 11 months ago

Actually, DoTween was failed for me because it didnt provide a good management on tween / sequence but this formula worked perfect "4 time - 4 time * time". If i fire a tween with a mouse call, i cant change the parameters till the tween finishes its job. I used this formula with a little change in update thus i obtain a parameter manuplation in jump actions. You know if i drop a control statement and check the time i want if(currentTime < desiredTime) then i use CalcJumpVector you posted in the comment above. Formula better than Tween :P

utsavll0 commented 11 months ago

Hey @Tehenauin I cant thank you enough. We have been stuck on this issue for so long, I came across your answer and it works like a charm Can you briefly explain how does the CalcJumpVector work? And how does that fit in the DoTween To function?

I am very sorry, for the late reply. unfortunately I have seen your comment just now.

The CalcJumpVector method interpolates a vector between the from and to Vectors just as it does in DOMove but it adds the jump vector with a multiplication dependent on the time.

You can read jumpVal = 4 * time - 4 * time * time; like this: y = 4x - 4x² its just a quatratic function that has a peak of 1 at 0.5 and is 0 when x is 0 or 1. Thats how it looks

The DoTween.To function is just the generic tweening funcion I have set the getter to () => 0f and the to value to 1 so it just interpolates between 0 and 1, and I can use the outcome as time for my CalcJumpVector Method.

I hope it is understandable. I am bad in explaining.

Ahhh now I see it. Amazing use of math concept. I need to really get good at using math concepts learnt in my 11th Grade. LOL. Anyways thanks again.

Maybe you should contribute this to the DoTween library so that they can add this as a feature so that the jump animation can be used generically in any axis, rather than just in the up direction

Tehenauin commented 11 months ago

Maybe you should contribute this to the DoTween library so that they can add this as a feature so that the jump animation can be used generically in any axis, rather than just in the up direction

Generally a good idea, but to be honest, I dont know how to integrate this properly. It feels more like a hack to me. In case someone else wants to do this, feel free :)