Mecanim - Detect End of state properly

So I need to know exactly when a state as ended to trigger another action.

So I have a state called “KickInHead” that transitions from Idle and once finished goes back to Idle.

var kickInHeadState : int = Animator.StringToHash("Base Layer.KickInHead");
var idleState : int = Animator.StringToHash("Base Layer.Idle");

It’s all setup correctly and works fine. Then I start the animation with a trigger and define a character state that I can use to update him once the animation has completed:

currentState = PlayerStates.BeatingUpActor;

Then, while the actor is in this new state I’m running this:

if (animController.currentBaseState.nameHash != animController.kickInHeadState){
	currentState = PlayerStates.Idle;

But what I noticed was that it immediately goes to 'PlayerStates.Idle, like the new state hasn’t started yet. I assumed the issue would be the transition, that in theory its not yet on the new ‘kickInHeadState’ but actually transitioning which makes sense. So I added this:

if (!animController.anim.IsInTransition(0) && animController.currentBaseState.nameHash != animController.kickInHeadState){
	currentState = PlayerStates.Idle;

And it still doesn’t work…he still believes I’m in Idle…so the only way I can do it is by writing and saying : leave this state IF back to Idle and Not in transition…

if (!animController.anim.IsInTransition(0) && animController.currentBaseState.nameHash == animController.idleState){
	currentState = PlayerStates.Idle;

And now it works fine…but it feels very messy to me, its like states are overlapping.
I assumed that ‘GetCurrentAnimatorStateInfo’ would give you only ONE state but it seems like the state machine actually has overlapping states on the same layer?

Any help would be very appreciated!

Just realized I asked the same question yesterday but much more articulate :slight_smile:
Also got a great reply to it.

You can read about it here: