Question about crossfading/blending a jump animation

Hi,

I have a low poly character with 3 animations: runforward, idle and jump

Based on the status of the UP cursor key I can make this character run forward and idle, but when I press spacebar , I want the character to JUMP.

Now jumping in the code below WORKS, but I have to keep the spacebar pushed down to let it finish the jump animation.

Question: Is there an easy way to change the code below so that I can force my jump animation to play when pushing down spacebar( even if its only for a very short while being pushed down) and then force the system to only start playing idle or runforward again after the jump animation is completely finished playing ?

CODE BELOW:


function Update ()
{
if (Input.GetAxis(“Vertical”) > 0.2) animation.CrossFade (“runforward”);
else
animation.CrossFade (“idle”);
if (Input.GetKey (“space”)) animation.CrossFade(“jump”);
}

anyone have an idea ?

Regards,

Bart

There’s a few ways, I think.

You could use a boolean variable to keep track of when the character is jumping.

//untested code fragment
if (Input.GetKey ("space")) jumping = true;
...
if (jumping == true) {
    animation.CrossFade("jump");
    if (animation.isPlaying() == false)
    {
        jumping == false;
    }
}

You could use a coroutine approach, also.

Thanks, will try this out