- Home /
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:
animController.anim.SetTrigger("isKickingHead");
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!
Answer by castor · Jul 18, 2014 at 06:14 PM
Just realized I asked the same question yesterday but much more articulate :) Also got a great reply to it.
You can read about it here: http://forum.unity3d.com/threads/mecanim-going-from-statea-stateb-statea-knowing-when-second-statea-has-started.257635/
you forgot that you had asked the question just the day after asking? o_0
Your answer
Follow this Question
Related Questions
Mecanim add state dynamically 0 Answers
Accessing a Mecanim state through scripting, then changing the associated clip--Can it be done? 2 Answers
yield while animation plays in mecanim 0 Answers
is it possible to read states / transitions from mecanim in runtime ? 1 Answer
saving class array states 1 Answer