使用 Mechanim Unity 快速按键播放动画

Playing an animation all the way through with one quick keypress with Mechanim Unity

我在 Update 函数中有一个 if 语句,只有满足其他三个条件时才会调用该语句。问题是现在该函数将一个布尔值设置为 true,这会导致动画在 unity Mechanim 中播放。只有当我按住按钮时这个布尔值才为真,但我希望它播放整个动画或在一定时间内保持这个布尔值为真,谢谢,这是我的代码。

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

我会使用 StartCoroutine

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

取决于您的 IEnumerator 参数?

IEnumerator WaitAndCallback(float waitTime){
    yield return new WaitForSeconds(waitTime);     
}

这是where我得到了示例代码

我在 Kelv.Gonzales 的帮助下找到了解决这个问题的方法。我让动画播放一段设定的时间,因为我知道它会持续 0.9 秒。这是这个问题的全部代码。

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