Home > Back-end >  My animations aren't playing when they should be
My animations aren't playing when they should be

Time:12-18

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

CodePudding user response:

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.

CodePudding user response:

SORRY I FOUND THE ANSWER I WAS DOING EVERYTHING TO A MODEL THAT WASN'T RIGGED

  • Related