Animation layers – creating masks

With Mecanim, it is also possible to blend animations for different body parts of a character. Let's see an example. You have a character that runs with a gun in its hand and you have a reload animation that the character performs each time its weapon is empty. When the character reloads, you want his upper body, actually its arms, to perform the reload action, while the lower body of the character, its legs, should keep running.

To achieve that in Unity, Mecanim features what are known as layers. Layers allow you to create masks for specific body parts of a character and apply animation clips only on those parts. A running animation that involves legs and arms can be blended with a reload animation that only involves the character's arms, the result being that the character keeps running with its legs, while its arms reload the weapon!

As layers require masks to work, in the next recipe, we will show you how to make one.

Getting ready

As we are not prototyping a 3D shooter here, we are not going to use actual animation clips for this recipe. We only show the required steps to use layers, for the time you will be willing to use them.

Have your project open in Unity; we left it with the Animator control panel containing the animations we scripted so far: Idle, Run, Walk, and Jump.

How to do it...

  1. If you take a look at the top-left corner of the Animator panel, you should see a box telling you we are working on Base Layer, as shown in the following screenshot:
    How to do it...
  2. The first thing to do is create an Avatar Mask out of the character avatar we are already using. Let's begin by creating a new folder in the project and name it Masks. We have done this operation several times; you won't need a reference picture…
  3. Now create a new Avatar Mask in this folder by right-clicking on the project panel and navigating to Create | Avatar Mask, as shown in the following screenshot:
    How to do it...
  4. Name the mask UpperBody.
  5. We now need to configure the mask by selecting the body parts that we want to be affected by our theoretical reload animation clip.
  6. Select UpperBody in the project panel and move it to Inspector. You should see two menu buttons: Humanoid (this is because we set our avatar to Humanoid when we imported this character's animation clips in Chapter 1, Importing 3D Models and Animations) and Transform.
  7. In the Humanoid panel, we have a representation of the character body. Right now, all body parts should be green, meaning that there is no selection made by the mask, yet. In this panel, green means that those parts are inside the mask and they will be affected by the layered animation clip, while red parts are out of the mask and the layered animation clip won't affect them. Clear enough?
  8. Click on the head, body, legs, and shadow under the feet and the four small IK red dots to exclude them from the mask, as shown in the following screenshot. Basically, only the arms and hands must be green.
    How to do it...
  9. Now expand the Transform menu by clicking on it. You should have an empty field in the Use skeletal from parameter (displaying None (Avatar)!), meaning no reference avatar is available to create the mask, yet.
  10. Click on the small button to the right to display the avatars available in your project and select one. At this point, you should have a CharAvatar available. Please refer to the following screenshot:
    How to do it...
  11. The mask is now saved and ready to be applied to a character.

How it works...

The principle to make masks is quite easy: starting from the actual avatar of your character, you can add or remove body parts to be included or excluded from those affected by specific animation clips. Thanks to Mecanim, the operation is quite transparent for the user! Still, many more settings are available that we didn't mention here, and we encourage you to refer to the manual, available at http://docs.unity3d.com/Manual/class-AvatarBodyMask.html.

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

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