I’m trying to create a hierarchical state machine, but don’t understand how to transition to a superstate

I have a CharacterController that implements a StateMachine that uses a number of states and transitions. The StateMachine checks for a valid transition to a new state at the start of every frame, and either moves to a new state or continues ticking the current state. Here is a brief example of all of this working together:

public class CharacterController : MonoBehaviour {     public StateMachine { get; private set; }           private void Awake()     {         InitializeStateMachine();     }      private void Update()     {         StateMachine.Tick(); // Checks for valid transitions, and then sets and/or ticks the appropriate state     }      private void InitializeStateMachine()     {         StateMachine = new StateMachine();          // Create states         var idleState = new IdleState(this);         var walkState = new WalkState(this);         var jumpState = new JumpState(this);          // Add some transitions         Func<bool> idleToWalk() => () => IsGrounded && velocity.x != 0;         Func<bool> idleToJump() => () => IsGrounded && Input.GetKeyDown(Keycode.Space);                                     // From       To       Condition         StateMachine.AddTransition(idleState, walkState, idleToWalk());         StateMachine.AddTransition(idleState, jumpState, idleToJump());     } } 

I really like this implementation because the states and the StateMachine are decoupled; only the "motor" class knows about both of these things, which makes it very easy for me to keep my code clean. Specifically, I like that the individual states don’t have state-switching code in them; then, if I want to make changes to how transitions between certain states occur, I have all of that code in one place (the InitializeStateMachine method, or I can add transitions in specific places later).

However, as my project grows I’m running into an issue where I’m having to create many transitions to states that share similar functionality. For example, the idle and walk states only work if my controller is grounded, so if I have a fall state then I need to make transitions from the fall state to every possible grounded state:

Func<bool> fallToIdle() => () => IsGrounded && velocity.x == 0; Func<bool> fallToWalk() => () => IsGrounded && velocity.x != 0; Func<bool> fallToSlide() => () => IsGrounded && velocity.x != 0 && Input.GetButtonDown("Slide"); 

It would be nicer if I could simply transition to some kind of generic GroundedState that handles the next steps as needed.

I read that one way to help solve this problem is to create a hierarchical state machine, but, though the concept makes sense to me, I don’t understand how to implement it in the context of my current system.

I assume that I would need to make a GroundedState parent class from which any ground states inherit, but how do I actually make transitions from fall to grounded, and then get from grounded to, for example, walk or slide in a single frame? Would the GroundedState need a reference to the StateMachine class so that it can set the correct substate accordingly?