Repository navigation
06. The state machine
In previous chapters we created a very simple 2 state animation character that simply switched between animations depending on its velocity. While this was easy to follow, it doesn’t scale well and the more the character increases in size, the more complex the animation logic becomes.
For these instances, PaperZD comes with a custom StateMachine node that is optimized for 2D usage. Let’s improve our walking character and migrate the logic into a new state machine.
First, we’ll create a new StateMachine node on the Animation Graph, we’ll call it Locomotion for the purpose of this example.


ℹ️ Note: As any other Animation Node, the
StateMachinecan be outputted to any other node and concatenated as we did in the previous chapter.
Double clicking the StateMachine allows us to modify its underlying graph, inside of it we’ll find a single Out node, that serves as the root of the state machine, from here we can start building our logic.

We will recreate the logic of the previous chapters first, for this we’ll drag from the Out node and put a State, this will be our Idle.


Double clicking on the state we’ll bring us to the state’s internal animation graph, which works just like the root graph we’ve been using; here we’ll simply put a Play Idle node.

After that, exiting this Animation graph back to the State Machine, we’ll drag from the edges of the state and create another State, we’ll call it Walking.

As you can see, an intermediate node has also been created, this is the Transition that will govern the logic that will trigger the state traversal from Idle to Walking. We’ll double click here and put the same logic we had on the previous example.

With this the Idle will change onto Walking whenever this condition is evaluated to true. We’ll need another condition for going back to Idle again, so we’ll drag from the edges of the Walking node onto Idle and put the reverse condition, also putting a Play Walk node on the state’s internal Animation graph


If we now press play, we can see how the state machine works exactly as the example in the previous chapters. We’ll expand on them now, but first need to modify our character and add some extra sequences.
First we’ll create the other animation sequences that we are missing, these are: Jump, Fall, Run and AirTrans.

And we’ll add a simple boolean into our character, that changes an IsRunning variable. This action should also change the characters movement max speed, but we’ll leave that out as we’re only interested in rendering rather than gameplay code. Inside the character we will add a Run mechanic and a Jump mechanic.

We’ll also go to our Animation Blueprints event graph and create a variable for our character, so we can query it when needed. We’ll use the event OnInit that gets called whenever the AnimInstance (class that this blueprint builds into) gets created.

Now, we can go into the state machine’s node Walk and change its internal animation graph to display either Walk or Run depending if the character is running

If we press play now, we’ll see that the character changes behavior and alternates not only between being Idle or Moving, but it can distinguish if it should run or walk. The logic for this is tightly packed in easy to modify sections and modular, which ensures that it’s as easy to iterate as it was to create.
