Creating Blend Tree

A very useful feature of the Mecanim system is the Blend Tree, which allows you to easily blend between two or more clips that can be controlled by a single float value. In fact, what if we plan to have our character switch between the idle, walk, and run states? With a Blend Tree, we can have the character's speed value take control of the conditions to switch between them, more or less as we did to switch between walking and running.

Follow us with the next recipe to learn how to use Blend Trees!

Getting ready

As we don't want to mess up the clips and transitions we created in the previous recipes, let's assume we are working with a clean animator controller. You can create a new one or clear the one we worked on before.

How to do it...

Access the Animator panel in Unity.

  1. Right-click anywhere in the window (which should only contain the Any State box) and select From New Blend Tree under Create State, as shown in the following screenshot:
    How to do it...
  2. Double-click on the Blend Tree you just created to access its properties and configure it.
    How to do it...
  3. Move to Inspector and click on the small + icon in the Motion field. Then, select Add Motion Field from the menu, as shown in the following screenshot:
    How to do it...
  4. Do this two more times, then start dragging the animation clips from the project panel into the Motion fields we just created in the order shown in the following screenshot:
    How to do it...
  5. We now need to set the thresholds to decide at which speed the character stops being idle and goes walking, and from there to running. Click anywhere on the blue area representing the transitions to activate the Threshold fields and type in these values, as shown in the following screenshot: 0.2, 0.5, and 0.6. The following screenshot shows the Motion section displayed in the Inspector window. Also, be sure that the fSpeed parameter we created before is selected from the Parameter drop-down menu!
    How to do it...
  6. You can check the result in the Preview window by clicking on the small play button. If the no model is available message appears, simply drag the character model into the window.

How it works...

With the help of a Blend Tree, we now have a very efficient way to control the animations of the character with regard to its speed. By checking its value, we can smoothly switch between idle, walk, and run. And it only took a few steps to achieve it!

We suggest you experiment with this tool and maybe repeat this last lesson to get more acquainted with Blend Trees.

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

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