You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用Animator时如何等待动画播放完成?

问题原因

调用animator.Play("Pop Down Animation")后,Animator组件不会在当前帧立刻刷新状态,此时调用GetCurrentAnimatorStateInfo(0)拿到的还是播放前的旧状态,导致WaitUntil的判断条件直接满足,立刻执行了后续的Play("New State"),前一段动画自然没有机会播放。

修复方案

在等待动画播放完成前先等待1帧,让Animator完成状态切换,同时增加目标动画状态的名称校验,避免误判:

IEnumerator popDownAnimation(string c)
{
    animator = GetComponent<Animator>();
    animator.Play("Pop Down Animation");
    // 等待1帧,让Animator完成状态切换
    yield return null;
    // 确认已进入目标动画,且播放完成、无过渡中状态
    yield return new WaitUntil(() => 
        animator.GetCurrentAnimatorStateInfo(0).IsName("Pop Down Animation") 
        && animator.GetCurrentAnimatorStateInfo(0).normalizedTime > 1 
        && !animator.IsInTransition(0));
    animator.Play("New State");
}

额外注意事项

  • 确认Animator Controller中Pop Down Animation状态没有开启循环,循环动画的normalizedTime会持续累加但永远不会触发等待结束的判断。
  • 如果你用的不是第0层动画,需要把GetCurrentAnimatorStateInfo和IsInTransition的参数改成对应动画层的索引。
  • 如果动画有固定时长,也可以用yield return new WaitForSeconds(动画时长)替代状态判断,适配简单场景。

内容的提问来源于stack exchange,提问作者user15603244

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.26 20:57:00