Skip to content

2.2 Creating a State Machine from scratch

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

Creating a State Machine from scratch

In this section, we'll see how the state machine from the previous example was built.

1. Creating the first State

Right click the project tab, and create a new state machine asset via Create -> DMotion -> State Machine. I named mine SM_SimpleExample. Double-click it to open the visual editor.

image

Let's firsts create the Walk State. Right-click the graph and select New State. A new single clip state will be create with a default name. Select it, rename it to Walk and for the clip select 2.1_StateMachineTransitions_Walk, which is a regular AnimationClipAsset that we introduced in the Getting Started section. Notice you can also edit the animation speed, and whether it loops.

image

Now go to the scene, select the LowPolyRobot and set your state machine as the State Machine Asset field. Hit play, you should see the robot playing the walk animation in loop.

image

2. Transitions and Parameters

Let's repeat the same process to create the Run state. Make sure to select 2.1_StateMachineTransitions_Run for the clip. Then right click the Walk state, select Create Transition, and then click the Run state. You should see a transition arrow created, if you click it you'll see the transition properties in the inspector tab.

image

It's useful to be aware of the Transition properties:

Property Definition
Has End Time Tells the system whether this transition should be played only after the State Has reached a specific time
End Time (only visible if Has End Time is true) The minimum time the state needs to reach before the transition can occur
Duration (s) Duration that the transition will be active and blending between blending the start state and the target state
Conditions List of conditions, based on Parameters that need to be met for the condition to trigger

In order to create transitions, we need parameters. Click the + button in the Parameters tab and select the Boolean option. Rename the parameter to IsRunning.

Now go back to the transition created earlier and add a Condition and select IsRunning as a parameter and True as the condition value. This tells the State Machine that it should transition from the Walk state to the Run state when the IsRunning parameter is True.

image

Now if you go to play mode, you should be able to transition between these states.

image

3. Wrapping it up

You probably notice that the Run animation is a little too fast (side-effect of using Creative Commons animations). Luckily, DMotion allows you to change the animation speed in the State Machine editor. Select the Run state and set Speed to 0.7.

image

Now the only thing left to do is to create a transition back to the Walk state. This follows the same process as the transition created in the previous step, but now using IsRunning equals False for the condition.

image

And that's all there is too it! DMotion's State Machine has many other features, but this should be enough to get you started. In the next section, you'll learn how to change parameters at runtime.