State Machine — Example
Last updated
Last updated
Let's see how the State Machine works in MANU using the example with a door.
First, we will add a Door and a Door State Machine to the Scene Tree via Toolbar. The size, position and rigidbody of the Door can be modified with the help of the Inspector Panel and its Tabs (Properties, Physics).
Then, we need to determine how many states and transitions between them our Door State Machine can have. Number of states of the State Machine are limited to the number of states we define in the game. Let our Door State Machine have 5 states, 5 transitions between them, and the initial state - Closed. Additionally, we will bind animations to states. So, it will help us see more clearly how the State Machine works in MANU.
States
Transitions
Closed
Opening
Open
Closing
Destroy
Closed → Opening
Opening → Open
Open → Closing
Closing → Closed
Any State → Destroy
Now, we will move on to creating states and connections between them. Open a Door State Machine by double-clicking on it in the Scene Tree.
Let's create a Closed and Opening states for our Door State Machine: rename “State 1” as “Closed"; add a new state and name it “Opening”. Using the Connector Tab on the Toolbar, create a connection between Closed and Opening.
Next step is to set the transition rule. Click on the dotted line and the Trigger Panel will appear. Using the Key trigger, we will create a transition rule for our Door State Machine from the Closed state to the Opening state. Select Key down, and enter 0 in the input field. This means that we have created the following transition rule: the door will start opening when key 0 is pressed. Now, the dotted line has become solid since the connection is configured. When you hover over the line, a description of the created rule will be shown (in our case, KeyDown: 0). Close the Door State Machine by clicking on the Closed button at the right end of the title bar.
Let's create an animation that will work with the state Opening. To add Animation to the Scene Tree, go to the Toolbar and click on the Animation icon. Let’s rename the Animation as “Animation Opening”. Now, switch to the Animation mode. Hover over the Door on the Scene Tree and add it to the Timeline using the context menu.
Let our door rise 2 meters up in 1.5 seconds, when its State Machine is in the Opening state. To do it, add an animatable parameter, clicking on the plus (+), which will appear if you hover over Door on the Timeline Panel (Properties → Position Y).
Now we need to create a trigger to start this animation. Switch to the Trigger Panel, and choose State Machine State. This trigger has 3 possible options:
State Machine State Start – the animation will start when the state of the state machine begins. For example, the animation starts, when the state machine has switched to the state Opening
State Machine State Stop - the animation will start when the state machine stops or changes (when the door opening state ends).
State Machine State Continue – the animation will continue while the state machine is in this door opening state.
For our Door, let’s choose State Machine State Start. Next, we need to select the State Machine to which we want to bind the Animation Opening: Select State Machine Select → Door State Machine.
Then, we select the required state of the Door State Machine: Select State → Opening.
As a result, we have our Door State Machine, which switches from the Closed state to the Opening state by hitting the 0 button. When the states change (Closed → Opening), the animation of the door opening starts. Let's switch to the Game mode, and press the 0 button. Did it work? 😊
Let’s also create an animation “Animation Closed” for the Closed state. Let’s say, our door changes color while the Door State Machine is in the Closed state. Add an Animation to the Scene Tree, and name it “Animation Closed”. Create a trigger to start this animation. Trigger/Timeline Panels appear when you click on the animation you need in the Scene Tree in the Animation mode. Go to the Trigger Panel, select State Machine State, then State Machine State Continue. This means that Animation Closed will run while the state machine is in the Closed state.
Next, you need to select the State Machine and the state itself to which you want to bind the Closed animation. Then, in the input fields, select:
Select State Machine → Door State Machine;
Select State → Closed.
Next, add the Door to Timeline; then, select an animatable parameter – Color. Let our door change color from white to pink while it is in the Closed state.
Ready! Switch to Game mode and see how our door changes colors while its State Machine is in the Closed state. But as soon as we press the 0 button, the animation will stop, because the Door State Machine has changed its state.
Let's create the state Open for our Door State Machine and a transition from the Opening state to the Open state.
Click on Door State Machine in the Scene Tree, add a new state in the pop-up window.
Click on the dotted line. Let's set up the rule when the door should switch from the Opening state to the Open state: when the Animation Opening ends. To do this, add an Animation trigger to the panel that appears; select Animation Stop from the drop-down list. Next, select the previously created Animation Opening. Thus, the transition of the Door State Machine from the Opening state to the Open state will occur when the Animation Opening ends.
For the Open state, we will create a new animation - Animation Open. Let the door turn green when the Door State Machine switches to the Open state.
Add a new Animation using the Toolbar , switch to the Animation mode, click on our new animation - Animation Open, then add the Door to the Timeline. Next, add an animatable parameter
Materials → Color, select the green color.
Switch to the Trigger Panel, select the State Machine State Start trigger. Next, select:
Select State Machine → Door State Machine;
Select State → Open.
In a game mode: after pressing the 0 button, the Door State Machine moved from the Closed state to the Opening state. The door opened; and the Door State Machine went from the Opening state to the Open state. As a result, the Door has turned green; the Animation Open has started.
Let's make a more interesting example when the door starts to close when the Main Character moves away from the door. To do this, you need to create a Collider. In the Toolbar, choose Collider. The size and position of the Collider can be changed by Gizmo Arrows or Properties Tab. Let's name the collider - Door Collider.
Now, go to the Door State Machine and create a connection between the Open state and the Closing state. We create a new rule: the transition from the Open state to the Closing state occurs when the collision of our Main Character and the Collider stops. In other words, when the Main Character leaves the collider space, moves away from the door.
To do this, click on the dotted line and choose the Collision Ended trigger. Then, we select the MainCharacter Body and the Door Collider as objects of the collision.
Now let's create an animation of closing the door in the Scene Tree (using Toolbar) and name it “Animation Closing”. In Animation mode, click on Animation Closing, add our Door to the Timeline, select an animatable parameter:
Properties → Position Y;
add the formula by which our door will close, i.e. returns to its initial position within one second (Position Y = origin -2).
Go to the Trigger Panel and create the trigger for Animation Closing. Select the State Machine
State trigger → State Machine State Start.
Then, choose:
Select State Machine →Door State Machine;
Select State → Closing.
Therefore, as soon as the state of the Door State Machine changes from the Open state to the Closing, the Animation Closing starts. And the transition from one state to another will occur when the Main Character leaves the Door Collider.
Let's switch to the Game mode and see what we've got. For visibility, you can enable the Physics Colliders mode.
We see that the door is closed, we press 0. After that, the door opens. Let the Main Character run under the Door. When he leaves the Door Collider space, the Door State Machine changes its state to Closing, and the Door begins to close (Animation Closing has started).
Now let's make the transition of the Door State Machine from the Closing state to the Closed state. For this purpose, we will use the Animation Closing: the Door State Machine will switch to the Closed state when Animation Closing ends.
In edit mode, open the Door State Machine and set the connection between the states Closing and Closed.
Let's set the rule for the state transition. Click on the dotted line, select the Animation Stop trigger. Next, select the animation you need, in this case, Animation Closing.
Let's switch to the Game mode. The Door State Machine is in the Closed state, so the door changes color from white to pink (Animation Closed). The Animation Opening will start when the Door State Machine switches from the Closed state to the Opening state. This will happen when we press the 0 button. After that, the Door opens and at the end of the Animation Opening, the Door State Machine switches to the Open state. When the Main Character leaves the collider space, the door will begin to close (Animation Closing) and the Door State Machine will have the Closing state. As soon as the Animation Closing is over, the Door State Machine will return to its initial state - Closed state.
We already know that Any State is a special state in the state machine; it means that a transition from any state can occur to one state. For example, in our case, the door can collapse in any state (opening, open, closing, closed).
Any State → Destroy
Let's now configure the transition from Any State to the new state of the Door State Machine - Destroy. This will mean that the Door State Machine can switch to the Destroy state from any state (in our case, from all 4 states). To do this, you need to create a Destroy state and use the Connector tool to draw an arrow from the Any State to the Destroy state.
Next, we set up the transition rule. Click on the dotted line between Any state and Destroy and go to the Trigger Panel. Let our door collapse when we press the G button. Select the Key Down trigger and type G in the input field.
Let’s prepare the animation of the door collapse and name it “Animation Destroy”. Inside the door we put 4 door blocks (boxes) that together make up the size of the door. These 4 objects should be physical and dynamic, as they will fall down when the door is destroyed.
Add 4 boxes to the Scene Tree using the Toolbar and drag them into the Door. Use the Inspector Panel to set the Physics, Position and Size of the new objects.
You may also add the Initial Velocities Features to make the objects fly apart when the Door is destroyed.
Disable boxes in the Scene Tree; so, they don't fall out as soon as the game starts.
Then, switch to the Animation mode, click on Animation Destroy in the Scene Tree and the Timeline Panel will appear. Add the Door and its boxes to the Timeline. For this animation, we need to disable the main door and to enable 4 boxes.
Next, go to the Trigger Panel. The Animation Destroy should start when the Door State Machine switches to the Destroy state. Therefore, choose State Machine State Start trigger, and then select:
Select State Machine → Door State Machine;
Select State → Destroy.
Let's see what we got! Go to the Game mode, press the G button and check if the door collapses when the Door State Machine is in the closed state. How about other states?
Thus, we have created the State Machine that has 5 states and 5 transitions between them. You can repeat all of this, as well as create more complex transitions and rules for your state machine.
Go for it!