0

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;
    }
}
ProgrammingLlama
  • 36,677
  • 7
  • 67
  • 86
Kai
  • 55
  • 6
  • 1
    Wouldn't it be better to use else if instead of ifs? Furthermore, it might be that there is some other case other than these 4 that might be messing with the animation. – mindoverflow Dec 18 '21 at 02:10
  • Looks like the animations could be being started every frame – BugFinder Dec 18 '21 at 09:36

2 Answers2

0

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.

  • 1
    thanks for the response! this script was from a tutorial and everyone in the comments seemed to say it worked fine and the video had it working fine but that may have just been something misleading. thank you so much and I will try using parameters and let you know! – Kai Dec 18 '21 at 02:16
  • wait actually i just realized, another thing was that when i am previewing the animations in inspector, there is no animation, it is just the character t-posing. i have already tried redownloading the animations from mixamo but still no luck. – Kai Dec 18 '21 at 02:17
0

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 :)

Kai
  • 55
  • 6
  • 1
    Wonderful! But stop shouting :) –  Dec 18 '21 at 04:02
  • sorry i just wanted people to see it early and not use their time to write an answer to something that has been solved. im still new to stack overflow i rarely use it :) thanks – Kai Dec 18 '21 at 20:18