Skip to content

2.1 State Machine: The Basics

Gabriel Dechichi edited this page Nov 7, 2022 · 2 revisions

State Machine: The Basics

DMotion comes with a native Animation State Machine runtime, debugger, and visual editor. If you're familiar with Unity's Animator Controller then you should feel pretty at home here.

In this section we will build a simple state machine that alternates between a Walk and Run state.

1. Playing the sample

Open the scene at All Samples/2 - State Machine/1 - Transitions/DMotion - State Machine - Transitions.unity and hit play. You'll see that the robot is walking in a loop. If you select the LowPolyRobot object, you will see AnmiationStateMachineAuthoring component, which is what connects the state machine to this object.

image

2. State Visual Editor Basics

Double click All Samples/2 - State Machine/1 - Transitions/2.1_StateMachine_Transitions.asset, it should open the visual editor. As mentioned above, this should feel similar to Unity's Animator Controller.

image

You can create your own State Machine via the create menu, at Create -> DMotion -> State Machine

  1. The Graph area

This is where you'll view and edit the state machine's States and their Transitions. Controls are the following:

Control Action
Left Mouse Click Select a State or Transition
Right Mouse Click Context menu for that clicked object. Clicking the graph shows options to create a new state
Left Mouse Drag Move a state around
Middle Mouse Drag (Or Alt + Left Mouse Drag) Pans the graph view
Mouse Scroll Zoom In/Zoom Out
Delete Key Delete selected state or transition
  1. Animation State

An animation state is a single clip, or a group of clips (Blend Tree) that the State Machine can transition to. The State Machine can only be at one state at a time, and the State Machine always needs a Default State (in orange), which will be the state that the State Machine will start at.

  1. Transition

Represents a transition between 2 states, and the conditions that will trigger this condition. It is possible to have more than one transition between 2 states.

  1. State Machine Parameter

DMotion state machine supports 4 types of parameters: bool, int, float and enum (Pro Only). These parameters are used as conditions for transitions, or as blend parameters for Blend Trees.

  1. Inspector View

Shows the properties for the selected State or Transition.

3. Visualizing the State Machine at Runtime

During development, it is sometimes crucial to be able to know what you're state machine is doing at runtime. DMotion makes it easier with a Visual Debugger.

In order to visualize the state machine at runtime, you need to do the following:

  • Make sure the State Machine Visual Editor is open (by double clicking a State Machine asset or going to Tools -> DMotion -> State Machine Editor)
  • Open the DOTS Hierarchy tab (Window -> DOTS -> Hierarchy)
  • Hit Play
  • Go to the DOTS Hierarchy tab and select the entity with the State Machine component (LowPolyRobot) in this case.
  • You'll see the current State Machine editor updating at runtime

image

The Visual Debugger both shows the current state, transitions, and values of parameters, but also allows you to change parameters at runtime to Debug the State Machine.

Try clicking the IsRunning parameter, you should see the transition between Walk and Run turn green for a bit, and then Run state becoming green after the transition completes.

image

Note: If you have code changing the parameters in a System OnUpdate, changing the parameter manually will not work, as the system will just override the parameter value every frame

4. Conclusion

That's the basics of State Machines in DMotion, in the next section we'll create a State Machine from scratch!