-
Notifications
You must be signed in to change notification settings - Fork 46
2.1 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.
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.
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.
You can create your own State Machine via the create menu, at Create -> DMotion -> State Machine
- 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 |
- 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.
- 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.
- 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.
- Inspector View
Shows the properties for the selected State or Transition.
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
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.
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
That's the basics of State Machines in DMotion, in the next section we'll create a State Machine from scratch!