简体   繁体   English

我的动画没有在应该播放的时候播放

[英]My animations aren't playing when they should be

So Basically, I am creating a wandering ai character in unity using c# and the wandering is all working fine, but when the certain animations are supposed to play, they don't.所以基本上,我正在使用 c# 统一创建一个流浪的ai角色,并且流浪一切正常,但是当某些动画应该播放时,它们却没有。 I will include the code I am using to make this happen.我将包括我用来实现这一点的代码。 I am also using an animator component on the model and the animations are all properly named and align with the animation in the animator's names and the animations are from mixamo.我还在 model 上使用动画组件,并且动画都已正确命名并与动画师名称中的 animation 对齐,并且动画来自 mixamo。 Any help is much appreciated because I am completely stuck!非常感谢任何帮助,因为我完全被卡住了!

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

Really consider using Animator parameters and transitions, it will save a lot of headache later on.真的可以考虑使用Animator参数和转场,以后会省很多麻烦。


Anyways, regarding the question: Your code is in Update , which means it runs every frame.无论如何,关于这个问题:您的代码在Update中,这意味着它运行每一帧。

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.这意味着您告诉 Animator 使用该名称播放 state 的每一帧,因此每一帧它都会重新启动 state 的 animation。 The result would be that your object would be stuck on the first frame of whatever animation.结果将是您的 object 将卡在 animation 的第一帧上。

Instead, you should call Play just once, when the desired conditions change.相反,当所需条件发生变化时,您应该只调用一次Play


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.顺便说一句,这是使用 Animator 参数更方便的一个示例,因为通过转换,您正在查询将 animator 从一个 state 带到另一个的特定条件,因此您将免费获得这种“只运行一次”行为.

SORRY I FOUND THE ANSWER I WAS DOING EVERYTHING TO A MODEL THAT WASN'T RIGGED IT'S ALL GOOD NOW THANKS FOR THE HELP:)抱歉,我找到答案了

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM