Skip to content

06. The state machine

David Parra edited this page Jan 16, 2026 · 1 revision

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.

39_Creating-more-complex-State-Machines

40_Creating-more-complex-State-Machines

ℹ️ Note: As any other Animation Node, the StateMachine can 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.

41_Creating-more-complex-State-Machines

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.

42_Creating-more-complex-State-Machines

43_Creating-more-complex-State-Machines

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.

44_Creating-more-complex-State-Machines

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.

45_Creating-more-complex-State-Machines

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.

46_Creating-more-complex-State-Machines

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

47_Creating-more-complex-State-Machines

48_Creating-more-complex-State-Machines

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.

49_Creating-more-complex-State-Machines

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.

50_Creating-more-complex-State-Machines

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.

51_Creating-more-complex-State-Machines

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

52_Creating-more-complex-State-Machines

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.

53_Creating-more-complex-State-Machines

Clone this wiki locally