Friday 29 January 2016

Blend Trees for smooth animations

Blend trees are a great tool that we can use in Unity if we want our characters to have smooth animations.

For this project I will use some of the standard asset, and this model.

Let's start by going in the Mobility_01_Free_v2 folder, FBX and locate the character model (MotusMan_v2). Before we drag it into the scene we need to change its rig. This is simply done by selecting the Rig tab, and change the Animation Type to Humanoid. Then click Apply. (Fig 1)


Fig 1

Now we create an Animator Controller and we assign it to our player. Let's also set the variable Apply Root Motion.

Open the Animator Controller window and create a new blend tree by right clicking and then select Create State -> from New Blendtree (Fig 2).

Fig 2

Now create 2 parameters of type float called Speed and Rotation, change the Blend Type to 2D Freeform Cartesian and assign the newly created float parameters in the fields below. (Fig 3).

Fig 3

Now we can start all the motion fields in the motion tab below. I will use the animations found in the standard asset package, in the folder StandardAssets -> Characters -> ThirdPersonCharacter -> Animation. Remember to drag in only the motion, not the whole prefab.We will need a total of 15 motion fields. You will see in Fig 4 which animations I picked and how I set them up.

Fig 4

Now, I could let the engine compute the values needed for the blending, based on the root motion, but I used another trick instead. Since we are going to feed in the blend tree variables (Rotation and Speed) with the Input.GetAxis("") method, we are only going to et values from 0 to 1.So, this is what I did: for each motion field, the Pos X parameter reresents our rotation. Therefore, I set it to 1 for every animation that involves a sharp rotation to the right, 0.5 for a "less sharp" rotation to the right. Same thing for the left rotation, but with negative values. Then, for the animation that requires no rotation, I set it to 0.

The other parameter, Pos Y, is our speed: I set it to 0 for every animation that has no forward movement, 0.5 for walking animations and 1 for running animations.

Depending on the values we are going  to feed in our blend trees, the engine will blend these animations together accordingly.

Now we can create the script that will pass values to the animator controller. It is actually very simple.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
public class PlayerAnimation : MonoBehaviour {

 public float dampSpeed;
 public float dampRotation;

 public Slider speedSlider;
 public Slider rotatioSlider;

 Animator anim;

 // Use this for initialization
 void Start () {

  anim = GetComponent<Animator> ();
 
 }
 
 // Update is called once per frame
 void Update () {

  float speed = Input.GetAxis ("Vertical")/2;
  float rot = Input.GetAxis ("Horizontal");

  if (Input.GetKey (KeyCode.Space))
   speed *= 2;

  dampSpeed = speedSlider.value;
  dampRotation = rotatioSlider.value;

  anim.SetFloat ("Speed", speed,dampSpeed,Time.deltaTime);
  anim.SetFloat ("Rotation", rot,dampRotation,Time.deltaTime);
 
 }
}

In this script we simply get the animator component (Line 14), and we pass it in the values of speed and rotation from the keyboard (Line 30,31). Notice how on line 21 I divide by half the value obtained, so we can only get a maximum of 0.5 (Input.GetAxis() returns a value from 0 to 1).
By doing so, we will assign a 0.5 to the Speed variable of the animator, triggering the walking animation. Then, if we press space (Line 24,25), I double the value of speed, bringing it to 1 and, therefore, triggering the run animation. A pretty cheap trick, but gets the job done.

Additionally, we apply damping to our animations. Damping is a way to smooth out the values passed to the animator. You can experiment with it in the example scene, as you can see the values for damping come from 2 sliders that you can modify while playing the scene so you will have a good idea of what damping is for.

At this point our character is ready and will move around our empty scene freely and in a very "smooth" way.

Example scene here.

2 comments:

  1. That's an interesting article! I'll keep reading your website. Added to it to my favorites bookmarks. Greetings.
    Hand Sketch Videos

    ReplyDelete
    Replies
    1. Hi, thank you very much. Unfortunately I don't have much time to post as frequently as I would love to these days, but I always keep an eye on the blog. I will try to publish more content. Meanwhile, thanks for the feedback, that really inspires me to work on this more!

      Delete