top of page

State Machine Behaviours

Using Unity's State Machine Behaviours allows you to add logic to a specific state, and can even be used to regulate transitions between states within the state machine itself. This can be used to compartmentalize the various behaviors and improve the modularity of the system overall.

The two basic enemy types really highlight the modular benefits of this system. Both the sword and the bow user share many of the same characteristics and use the same state machine. Variables like the attack range can be different between them, but the same logic can still be applied for the state transition. You can combine State Machine Behaviours for transitions with regular transitions, which is how the hit mechanic was implemented. The characters can be hit from any state without needing to track that in other behaviors, as you only need to activate the hit trigger variable externally. Exiting logic for whatever state the character was currently in still takes place thanks to the OnStateExit method within the behavior.

​

The boss was more complex. He essentially is made of three AI segments that all operate independently from each other. The head has targets which serve as the hands' rest positions, where they return to after finishing an attack. The head itself moves to follow the player, and as a result the hand rest positions move with it, keeping the hands close to the head as they return. Since the hands are outside of the hierarchy for the head, their motion does not get affected by the motion of the head.

bottom of page