如何在Unity C#中等待动画完成

4

我想在设置触发器后等待动画片段完成。

private void Start()
{
    anim = GetComponent<Animator>();
}


private IEnumerator Die()
{
    // Play the animation for getting suck in
    anim.SetTrigger("Shrink")      

    // Wait for the current animation to finish
    while (!anim.IsInTransition(0))
    {
        yield return null;
    } 

    // Move this object somewhere off the screen

}

动画播放了,但在while循环后的代码执行前动画并不会等待完成。
下面是我的动画组件:enter image description here
2个回答

2

1
链接已经失效,截至2021年10月,这个链接可以使用 https://docs.unity3d.com/Manual/script-AnimationWindowEvent.html - Felipe

1

你可以等待动画完成的时间,这个时间可以在检查器中找到,看你的动画需要多长时间才能完成,然后在触发动画后等待该时间即可。

因此,在你的情况下,它会像这样:

private IEnumerator Die()
{
    // Play the animation for getting suck in
    anim.SetTrigger("Shrink") 

    yield return new WaitForSeconds(animationTime);

    // Move this object somewhere off the screen

}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接