0
\$\begingroup\$

I have an if statement inside of the Update function that is only called if three other conditions are met. The problem is that right now the function sets a boolean to true which causes the animation to play inside of unity Mechanim. This boolean is only true when I hold down the button, but I would like it to play the whole animation or keep this boolean true for a certain amount of time, thanks and here is my Code.

    //Running and jumping animation
    if (Input.GetKey (KeyCode.W) && (Input.GetKey (KeyCode.Space)) && otherAnimation == false) {
        anim.SetBool ("isRunningAndJumping", true);
    } 
    else {
        //anim.SetBool ("isRunningAndJumping", false);  
    }
\$\endgroup\$

1 Answer 1

0
\$\begingroup\$

I figured out how to solve this problem with the help of Kelv.Gonzales. I let the animation play for a set amount of time as I knew that it would last 0.9 seconds. This is the whole piece of code for this problem.

//Running and jumping animation
    if (Input.GetKey (KeyCode.W) && (Input.GetKeyDown (KeyCode.Space)) && otherAnimation == false) {
        anim.SetBool ("isRunningAndJumping", true);
        StartCoroutine(WaitAndCallback(0.9f));
        //anim.SetBool ("isRunningAndJumping", false);
    } 
    else {
        //anim.SetBool ("isRunningAndJumping", false);  
    }

IEnumerator WaitAndCallback(float waitTime){
    yield return new WaitForSeconds(waitTime);
    anim.SetBool ("isRunningAndJumping", false);      
}
\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .