Scripts cant keep up with Animator

Hello,

Basically what I am trying to do is keep checking where the current animation’s normalized time is and if its greater than 0.9f then do the code that is supposed to happen when the animation is done.

void Update()
{
    Animator anim = GetComponent<Animator>();
    AnimatorStateInfo stateinfo = anim.GetCurrentAnimatorStateInfo(0);

    if (stateinfo.IsName("Attack1"))
    {
         if (anim.GetCurrentAnimatorStateInfo(0).normalizedTime > 0.9)
         {
            print("It's done!");
         }
    }
}

now with longer/slower animations the script can keep up with the pace and do the code but with shorter/faster animations it moves too fast for the script to keep up with it…

How do you suppose i can fix this?

I would suggest you to use AnimationEvents for this kind of setup. Create an animation event at the end of your clip and do you code in this function, because if you plan to have a lot of state that does the same kind of thing you will end up with a lot of code in your update function which is called at each frame. At the end your code will run faster and your Update function will be more clean.

If your clip is set to loop, the integer part of normalizeTime should increase by 1 each time the clip loop

If your clip is not set to loop, when the animation finish to play normalizeTime should stay at 1.0.

So i’m not sure why the value is never bigger than 0.9, maybe another script play another clip?

First off, Thank you for replying!

Oh I will test the animation events thing now and tell you the results :smile:.

“So i’m not sure why the value is never bigger than 0.9, maybe another script play another clip?”

If what i am thinking is correct then it never detects the greater than 0.9 because the animation state changes before the script can check whether its greater than 0.9 and when the animation state changes the state.IsName(“Attack1”) becomes false.

Edit: Used the Animation Events and it works like a charm :), thnx alot for ur help!

Do you have a transition that goes from state ‘attack’ to another state with an exit time greater than 0.9 with a shot duration? if yes that could be the case.

No, there is only 1 transition from “Attack1” and that is with a boolean variable.

But that is ok, the Animation Events did the trick! thanks alot for you help!