Search Unity

Question StateMachineBehaviour: OnStateEnter/OnStateExit called multiple times using substates

Discussion in 'Animation' started by slippyfrog, Oct 31, 2023.

  1. slippyfrog

    slippyfrog

    Joined:
    Jan 18, 2016
    Posts:
    42
    Setup

    upload_2023-10-30_23-8-33.png

    Attached to 'New StateMachine' is the following class

    Code (CSharp):
    1. public  class TestState : StateMachineBehaviour
    2. {
    3.     [SerializeField]
    4.     private string m_Tag = "Default";
    5.  
    6.     public override void OnStateEnter(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
    7.     {
    8.         Debug.Log ("OnStateEnter >> " + m_Tag);
    9.     }
    10.  
    11.     public sealed override void OnStateUpdate(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
    12.     {
    13.     }
    14.  
    15.     public override void OnStateExit(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
    16.     {
    17.         Debug.Log ("OnStateExit >> " + m_Tag);
    18.     }
    19. }

    'New StateMachine' is a sub-state Machine as follows:
    upload_2023-10-30_23-10-16.png


    When the substate machine transitions from 'New State' to 'New State0' to 'New State 1', the OnStateEnter/OnStateExit of attached to the parent state machine get called.

    upload_2023-10-30_23-11-23.png

    Intuitively, one would assume that the OnStateEnter/OnStateExit do not get called when transitioning between substates of a sub-state machine.


    I'm just curious if any one knows of the justification for this design decision. and if there is a way to get the behaviour I'm looking for:


    When attaching a StateMachineBehavior to a parent state machine, the OnStateExit/OnStateEnter do not get triggered when the sub-states of the state machine are transitions. These methods only get triggered when the parent state transitions.

    As a workaround, I guess I could do name compares in the OnStateEnter/OnStateExit but that seems a bit clunky. not sure if this is the ideal solution though. It seems like others would have solved this issue as it feels like a common use case to me.

    Thanks for any help/insight.
     
    Last edited: Oct 31, 2023
  2. slippyfrog

    slippyfrog

    Joined:
    Jan 18, 2016
    Posts:
    42
  3. LunarEclipse

    LunarEclipse

    Joined:
    Sep 5, 2017
    Posts:
    3
    same problem:(