0

所以基本上,我正在使用 c# 统一创建一个流浪的 ai 角色,并且流浪一切正常,但是当某些动画应该播放时,它们却没有。我将包括我用来实现这一点的代码。我还在模型上使用了动画器组件,并且动画都正确命名并与动画师名称中的动画对齐,并且动画来自 mixamo。非常感谢任何帮助,因为我完全被卡住了!

   void Update()
    {
    if (isWandering == false)
    {
    StartCoroutine(Wander());
    }
    if (isRotatingRight == true)
    {
        gameObject.GetComponent<Animator>().Play("idle");
        transform.Rotate(transform.up * Time.deltaTime * rotSpeed);
    }
    if (isRotatingLeft == true)
    {
        gameObject.GetComponent<Animator>().Play("idle");
        transform.Rotate(transform.up * Time.deltaTime * -rotSpeed);
    }
    if (isWalking == true)
    {
        gameObject.GetComponent<Animator>().Play("waalk");
        transform.position += transform.forward * moveSpeed * Time.deltaTime;
    }
}
4

2 回答 2

1

Really consider using Animator parameters and transitions, it will save a lot of headache later on.


Anyways, regarding the question: Your code is in Update, which means it runs every frame.

That means every frame you're telling the Animator to play the state with that name, so every frame it starts the animation of that state over again. The result would be that your object would be stuck on the first frame of whatever animation.

Instead, you should call Play just once, when the desired conditions change.


Incidentally, that's one example of when it would be more convenient to use Animator parameters, since with transitions you are querying for specific conditions that take the animator from one state to the other, so you would get this "run just once" behaviour for free.

于 2021-12-18T02:14:52.487 回答
0

抱歉,我找到答案了

于 2021-12-18T02:28:41.030 回答