Setting up an Animator

Double-click on Packt_Animator from the Project panel to open the Animator panel in Unity. If you messed up a little with it, delete anything except the default Idle node.

How to do it...

  1. Create three new empty states by right-clicking anywhere in the Animator window, then select Create State and Empty, as shown in the following screenshot. Name them runRight, runLeft, Idle and Jump.
    How to do it...
  2. Add the run animation clip to both runRight and runLeft states, as shown in the following screenshot:
    How to do it...
  3. Lastly, add the jump_pose clip to the Jump state. Here's a screenshot, just in case:
    How to do it...
  4. Now we can take care of the transitions. First of all we need two variables: a float called fSpeed to check whether we should trigger the left or the right sided running animation, and a Boolean called bJump for jumping. You should remember how to do this. The following screenshot shows what you are searching for:
    How to do it...
  5. Create a new transition from Idle to runRight, and in the Inspector panel, set the condition to fSpeed greater than 0.2, as shown in the following screenshot:
    How to do it...
  6. We also need to create a transition back from runRight to Idle and set the condition for triggering the transition as follows: we trigger the animation when the fSpeed parameter has a value below 0.2. Check the following screenshot to be sure you are doing things right:
    How to do it...
  7. Repeat the same operation with Idle and runLeft, but this time the enter condition is that fSpeed is less than -0.2, while the exit condition is that fSpeed is greater than -0.2.
    How to do it...

    The reason for this is that when the character runs left, it is going towards negative values on the x axis and thus we need to check for negative values of fSpeed!

    How to do it...
  8. For the Jump animation, we use the bJump parameter. Create a transition from Idle to Jump and set bJump to true as its entering condition, as shown in the following screenshot:
    How to do it...
  9. As for the exit transition, create a new one back from Jump to Idle and set bJump to false as its condition. You can refer to the following screenshot:
    How to do it...
  10. Repeat the same operations connecting runRight and runLeft to Jump to complete the task.

How it works...

With the Animator updated, the character now triggers the right running animations depending on its direction and a jump pose upon jumping. For the prototype, we don't need anything more than this.

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

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