Search code examples
animationunity-game-engineunityscript

My animation is lagging, why?


I have an animation in unity, and basically it shows Donald Trump running:

enter image description here

I also have this one frame animation of Trump jumping:

enter image description here

Basically, when he jumps, the jump animation plays, and when he lands, the walk animation plays again.

enter image description here

This all works, and this code runs it:

function Update() {
    trump.velocity = Vector2(speed, trump.velocity.y);
    if (jump > 0) {
        jumpBool = true;
    }
    else {
        jumpBool = false;
    }
    animator.SetBool("Jump", jumpBool);

That's in the physics script. Then from the animator:

enter image description here enter image description here

This all works, and the animations change when they are supposed to. The issue is, it lags before it finishes. I think that when Trump jumps, the walk animation finishes before it switches to the jump animation. My question is, how do I automatically switch animations immediately, so it won't look so laggy?


Solution

  • You can immediately call the Jump animation to be played at the point you make the JumpBool = true. Doing so you don't need to wait for the walk animation finish, it will simply stop the Walk and and move to Jump.

    function Update() {
        trump.velocity = Vector2(speed, trump.velocity.y);
        if (jump > 0) {
            animator.Play("Trump Jump");
            //jumpBool = true;
        }
        else {
            //jumpBool = false;
        }
        //animator.SetBool("Jump", jumpBool);    
    

    enter image description here

    You don't even need to Set the bool, after the Jump animation has finished it will move back to the Walk animation.

    enter image description here