- Home /
 
State Machine Behaviour OnStateEnter Check previous state?
Hey, I want call OnStateEnter on a state but only if it transitioned from a specific state and not from all the states connected to it, how can this be done? I just want to call a function if a specific state is transitioning to another specific state, thanks.
Answer by Khena_B · Mar 16, 2020 at 07:02 PM
So I came up with my own solution for now, if you have a better one post it as an answer and I'll change it. What I ended up doing was getting the name hash every late update, so that I can check what state it was in the previous frame.
 void LateUpdate()
 {
     previousStateHash = animState.shortNameHash;
 }
 
               And then any Behaviour scripts can check if the previous state was a state in particular by doing:
 override public void OnStateEnter(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
 {
     if(previousStateHash == Animator.StringToHash("Some_State"))
         Debug.Log("Transitioned from Some_State");
 }
 
              Your answer
 
             Follow this Question
Related Questions
moving gameobject from state machine behaviour/different code result from inside OnStateExit() 1 Answer
Interface state structure with different type of enemies 0 Answers
Mecanim empty state has unwanted delay 1 Answer
How can I switch between prefab variants in runtime. 1 Answer
Animator transition from Entry state to multiple states in Unity 5 4 Answers