-
Notifications
You must be signed in to change notification settings - Fork 0
State Machine
This is a simplified explanation of the State Machine, focused on the class structure and its usage.
More in-depth information can be read in the original State Machine Wiki for the Unity Open Project #1: Chop Chop project.
The changes in this project were done to keep it updated to the latest Unity versions and to decoupled it from the rest of the original project code and folder architecture.
The state machine is designed through Scriptable Objects. During the runtime, that definition is used as a blueprint to instantiate the state machine inner components.
When creating your agent, the recommended workflow is to use:
- Passive
MonobehaviourComponents with public properties, functions and events to gather information or run specific behaviors. -
StateActionSOassets to set the Inspector properties that will be used byStateAction. -
StateActioninherited classes to run the behaviors from those Components and Assets. -
StateConditionSOassets to set the Inspector properties that will be used byStateCondition. -
StateConditioninherited classes to make decisions on state changes based on information from those Components and Assets. -
StateSOassets to define a state and it's actions. - A
TransitionTableSOasset to glue all states with transitions and conditions. - A
StateMachineComponent to instantiate and control the states during runtime.
- It holds a collection of Actions and Transitions that are automatically filled during runtime.
- It doesn't need to be implemented, as there is no customization needed.
- Classes inherited from it will continuously execute the actual runtime code for your agent while the State is active.
- Actions are called during the state machine
Awake,Update,OnStateEnterandOnStateExit. - This is where you can move the character, update the animation, etc.
- Classes inherited from it must override the
Condition.Statement()function. - This is where you can evaluate the current situation of the agent on thing like ground checks, movement direction, colliding with things, etc.
- Statement results are cached during a single frame, allowing the transition logic to check them multiple times without impacting performance.
-
StateConditionis a wrapper that will convert the statement output to the result expected by the transitions.
- It's responsible for analyzing when a state should transition to another. It holds a target State and groups of Conditions.
- Conditions are grouped based on their boolean relationship using AND (grouped) and OR (ungrouped).
- It doesn't need to be implemented, as there is no customization needed.
- This is the Component to be placed in a GameObject agent.
- It only requires a
TransitionTableSOasset to define how the state machine will be set up during runtime. - In Editor Mode, it also accepts a reference to the
StateMachineDebuggerto help diagnose faulty state transitions. - Actions and Conditions should access the GameObject Components through the state machine functions as they are cached, for improved performance.
- It represents a
Stateand contains a collection ofStateActionSOthat must be supplied by the user. - It doesn't need to be implemented, as there is no customization needed.
- It represents
StateActionsand must be implemented for every type of action. - This is also where the action parameters are set. Use the asset to expose properties to the Inspector.
- There is a non-generic version, but the generic class is preferred.
- It represents
StateConditionsand must be implemented for every type of condition. - This is also where the condition parameters are set. Use the asset to expose properties to the Inspector.
- There is a non-generic version, but the generic class is preferred.
- This is the heart of the State Machine, it defines all transitions for a State Machine.
- Although it is possible to edit the asset directly, the State Machine Editor Window offers a much easier experience.
This window, accessible through the menu Tools > State Machine Editor, is the main tool to compose a state machine.
With it, you can change the state machine logical design:
- Adding actions to a state.
- Adding state transitions.
- Adding a target state and conditions to a transition.
- Setting the condition order, logic operation and boolean transformation.
- It is not possible to group conditions manually (like using parentheses), but conditions can be repeated to create a similar logic.
- Boolean transformation can convert a false output to true. This separates the condition result to the transition expected value.
This class can debug the state transitions. It's extremely useful when the state machine goes haywire and start to transition in unexpected situations.
The debugger can be enabled and disabled during play mode.
The debugging details contain the agent, the current state, the target state and the condition results.