Animation transition ready event?

Hi all,

is there a reliable way to determine if a transition from one animation to the next one has finished?
My problem is as follows:
I got an idle animation, running, stopping and aiming (raising a crossbow).
I also got internal states for the player (an enum with isIdle, isRunning etc.)
When I start running the running animation is played, the internal state of the player is set to isRunning.
When I stop running, the stopping animation is played, the internal state is isStopping and at the end of the animation the animator automaticaly plays the idle animation again and I fire an animation event so that my internal state can also switch to isIdle again. This works fine.
When inIdle, I can press the mouse button to aim my crossbow at something, the animation gets played and internal state is isAiming. When I release the mouse button, there is a transition back to idle. But my internal state is immediately set in code to isIdle. This is a problem because the player can then start running. This is not seen in the animation since the animator is still in transition to the idle animation. But the internal states start to differ and since there is no running animation there will be no stopping animation and no animation event to help getting from isStopping to isIdle again.
What I would need is an event telling me the transition from aiming to idle is ready, the animator can now receive all the input for the idle state. I tried to use GetAnimatorTransitionInfo.normalizedTime to check when it reaches 1 but this is not reliable, I guess when it is 1, the info gets deleted hence there is no transition anymore. Anyway I can’t check for that.
Another option would be to create a stoppingAiming animation like with the running and let that fire an event and transition very fast without blending to idle animation again. But that sounds like a lot of unnecessary work.

How do you keep your animator states and internal player states in synch?

Thanks in advance, this is driving me crazy! :slight_smile:

There are several options but first of all dont force any animation through code.
Place an if statement somwhere that checks if your aiming animation is finished then go to next state.
In addition you can set interrupts in your transition: setting → interruption source
Also you should use the Animator parameters to decide which animaton should play.
Create a bool parameter in Animator Window → Mouse down sets that bool:
Animator.SetBool(“aim”,true);
Mouse up resets that bool:
Animator.SetBool(“aim”,false);
And transition to idle begins.
If you want some input to be read in certain State, then do something like this:
if (GetComponent().GetCurrentAnimatorStateInfo.IsName(“isIdle”)){
//read input
}

You can also use a StateMachineBehaviour script.
(Click on StateMachine in Animator → add Behaviour → new script)
There you can find three main Events:
OnStateEnter
OnStateUpdate
OnStateExit: this will be executed once on the last frame of animation in a transition.
you can set a parameter variable here and check for it in your code.

Hope that helped

Thanks a bunch that did work very well! Is already used IsName() but was too stupid to use it the right way. Interruption source and StateMachineBehaviour are also a good hint for further research.
Thanks again!