Unity 动画 - yield return new WaitForSeconds(secs) 不工作

Unity animations - yield return new WaitForSeconds(secs) not working

为什么我使用这段代码时动画在Unity中不播放?我怎样才能让程序等待动画结束?在 JavaScript(或 UnityScript)中,这种方法奏效了。

public bool Attacking { get; set; }
private Animator anim;

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

private IEnumerator Attack()
{
    Attacking = true;

    anim.SetTrigger("Attack"); // this is not playing

    yield return new WaitForSeconds(attackLength);

    Attacking = false;
}

这就是你 运行 协程的方式:

StartCoroutine(Attack());

StartCoroutine("Attack");

如果您尝试 运行 它像通常的功能一样它不会工作。

所以这是解决方案:

public void PlayAttack() 
{
    StartCoroutine(Attack());
}

Learn More