【问题标题】:Playing an animation all the way through with one quick keypress with Mechanim Unity使用 Mechanim Unity 快速按键即可全程播放动画
【发布时间】:2016-10-18 19:37:42
【问题描述】:

我在 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);  
}

【问题讨论】:

  • 如果您获得 Input.GetKeyDown 而不是 Inpt.GetKey ,您是否检查过行为是否有所不同?
  • 是的,当我这样做时,动画在我按下键时开始播放,但在那之后立即停止播放。我希望它在发生这种情况时播放整个动画。
  • 前面的评论说明了未提交底线会发生什么。将该行保留为注释的结果将继续播放动画,因为布尔值将保持真实。
  • 如果你知道动画有多长,可以试试yield return new WaitForSeconds("animation time without quotes")
  • 如何在我的代码中实现这一点?我是否必须创建另一个 IENumerator 函数或其他东西。你能给我举个例子吗?

标签: c# unity3d boolean unity5


【解决方案1】:

我会使用 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我得到了示例代码

【讨论】:

  • 而“gobool”代表什么?
  • 我仍在试图弄清楚,我认为这与他们自己的代码有关,他在其中创建了一个 bool 并检查它。我会整理的。
  • 我明白了。先生,你应该得到一块饼干。非常感谢!我将在此处发布代码。
【解决方案2】:

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

【讨论】:

    猜你喜欢
    • 2023-02-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-21
    • 2022-01-23
    • 2015-04-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多