Building the Animation State Machine

If you navigate to the Animation folder, you will not only find all your animations but also an Animator (in case you find more than one, delete them all but one). First of all, rename it Panda_Animator, and double-click to open it. You will end up in the Animator editor. You should have all the animations created in the previous sections as disconnected nodes. In case you don't have these animation nodes (if you had to delete the other animators), just drag the animations from the Project panel into the Animator. Feel free to rearrange them as you like; here is how I arranged them in order to not make transitions cross each other later on:

As you can see, there are all your animations already, but they are not connected by any transitions. Before we continue, think about how they should be connected, then continue to read here.

First of all, it's not fine that from the Entry node we go directly into the Walk node. In fact, we need to start with the Idle node. Hence, if Idle is not highlighted, then right-click on it and select Set as Layer Default State, as shown in the following screenshot:

As a result, the Idle animation should be highlighted, and an arrow should go from the Entry node to the Idle one:

Next, from the Idle node, we can go both to the Jump_Start (because the character can jump when the player is not moving), or to the Walk node (because the character can start moving). As such, with the right-click and then Make Transition, create these two arrows. So far, you should have something similar to this:

Now, it's time to connect the other nodes. From the Walk node, we can go back to Idle, in case we stop moving, or jumping, and thus go to Jump_Start. Make these other two transitions, and you will end up with the following:

From the Jump_Start node, we cannot go back to Idle or Walk because we need to finish the Jump loop, so we need to go first to Jump_Air and then from there to Jump_Landing. This is the result so far:

Finally, from the Jump_Landing, we need to go back to Walk or Idle (depending on whether the character is moving or not), and thus create two arrows towards them. This is the final result:

However, we haven't finished yet. In fact, we didn't establish any rule on how to fire the transitions. To do so, we need parameters. Go to the Parameter tab in the upper-left corner and create a couple of parameters. One parameter is of type Float and we will name it Speed; the other one is a Boolean and we will name it Jump. The idea is to have the Speed stored in the float parameter, to determine whether the character is moving or not, and in the Jump Boolean to determine whether the character is performing a jump or not.

Here is a screenshot of how the list of the parameters should appear:

Now, we need to set all the transitions properly. If you select an arrow, in the Inspector you will see all the properties, and at the bottom, you can set conditions on when the transition is fired.

Following is the screenshot highlighting where to insert a condition on a transition:

To properly set all the conditions for our Animation State Machine, follow these instructions, transition by transition:

  • Idle - > Walk: Triggers if the Speed parameter is greater or equal than 0.01.
  • Walk - > Idle: Triggers if the Speed parameter goes below 0.01.
  • Idle - > Jump_Start: Triggers if the Jump Boolean becomes true.
  • Walk - > Jump_Start: Triggers if the Jump Boolean becomes true.
  • Jump_Start - > Jump_Air: Triggers as soon as the Jump_Start animation has finished, thus set the Exit Time to 1 and Transition Duration to 0, no further conditions.
  • Jump_Air - > Jump_Landing: Triggers as soon as the Jump Boolean becomes false.
  • Jump_Landing - > Idle: Triggers as soon as the Jump_Landing animation is finished (so set Exit Time to 1 and Transition Duration to 0, but on the condition that the Speed parameter is less than 0.01).
  • Jump_Landing - > Walk: Triggers as soon as the Jump_Landing animation is finished (so set Exit Time to 1 and Transition Duration to 0, but on the condition that the Speed parameter is greater or equal than 0.01)

That was quite a lot of work, but we have finished with our state machine. The next step is to build a controller that is able to take care of this state machine, by setting the Speed and Jump parameters. But for now, take a rest and read the summary of this chapter, before we have fun in the next one.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset