Dealing with transitions

In this recipe, we create and set up the transition for the character to switch between the Idle and Jump animation clips. For this task, we also need a parameter, which we will call bJump, to trigger the jump animation through code.

Getting ready

We will build on the previous recipe. Have the Animator window open, and be ready to follow our instructions.

How to do it...

  1. As you move to the Animator panel in Unity, you should see a orange box representing the Idle animation, from our previous recipe. If it is not, right-click on it, and from the menu, select Set As Default. You can refer to the following screenshot:
    How to do it...
  2. Right-click on the Idle clip and select Make Transition from the menu, as shown in the following screenshot:
    How to do it...
  3. Drag the arrow that appears onto the Jump clip and click to create the transition. It should appear in the Inspector window, to the right of the Animator window. Check the following screenshot to see whether you did it right:
    How to do it...
  4. Now that we have got the transition, we need a parameter to switch between Idle and Jump. We use a boolean type for this, so we first need to create it. In the bottom-left corner of the Animator window, click on the small +, and from the menu that appears, select Bool, as shown in the following screenshot:
    How to do it...
  5. Name the newly created parameter bJump (the "b" stands for the boolean type; it's a good habit to create meaningful variable names).
  6. Click on the white arrow representing the transition to access its properties in Inspector. There, a visual representation of the transition between the two clips is available.
  7. By checking the Conditions section in Inspector, you can see that the transition is right now controlled by Exit Time, meaning that the Jump clip will be played only after the Idle clip has finished playing. The 0.97 value tells us that the transition is actually blending between the two clips for the last 3 percent of the idle animation. For your reference, you can adjust this value if you want to blend it a bit more or a bit less. Please refer to the following screenshot:
    How to do it...
  8. As we want our bJump parameter to control the transition, we need to change Exit Time using the tJump parameter. We do that by clicking on the drop-down menu on Exit Time and selecting tJump from the menu, as shown in the following screenshot:
    How to do it...
  9. Note that it is possible to add or remove conditions by acting on the small + and - buttons in the interface if you need extra conditions to control one single transition. For now, we just want to be sure that the Atomic option is not flagged in the Inspector panel. The Atomic flag interrupts an animation, even if it has not finished playing yet. We don't want that to happen; when the character jumps, the animation must get to its end before playing any other clip.

    The following screenshot highlights these options we just mentioned:

    How to do it...

How it works...

We made our first transition with Mecanim and used a boolean variable called bJump to control it. It is now possible to link bJump to an event, for example, pressing the spacebar to trigger the Jump animation clip.

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

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