Mixing animations with Layers and Masks

Mixing animations is a great way of adding complexity to your animated characters without requiring a vast number of animated clips. Using Layers and Masks, we can combine different animations by playing specific clips for the specific body parts of the character. In this recipe, we will apply this technique to our animated character, triggering animation clips for firing a rifle, and throwing a grenade with the character's upper body. We will do this while keeping the lower body moving or idle, according to the player's input.

Getting ready

For this recipe, we have prepared a Unity Package named Mixing, containing a basic scene that features an animated character. The package can be found inside the 1362_07_03 folder, along with the animation clips called Swat@firing_rifle.fbx and Swat@toss_grenade.fbx.

How to do it...

To mix animations using layers and masks, follow these steps:

  1. Create a new project and import the Mixing Unity Package. Then, from the Project view, open the mecanimPlayground level.
  2. Import the Swat@firing_rifle.fbx and Swat@toss_grenade.fbx files to the project.
  3. We need to configure the animation clips. From the Project view, select the Swat@firing_rifle animation clip.
  4. Activate the Rig section. Change Animation Type to Humanoid, and Avatar Definition to Create From this Model. Confirm this by clicking on Apply.
    How to do it...
  5. Now, activate the Animations section. Select the firing_rifle clip (from the Clips list), click on the Clamp Range button to adjust the timeline, and check the Loop Time and Loop Pose options. Under Root Transform Rotation, check Bake Into Pose, and select Baked Upon | Original. Under Root Transform Position (Y), check Bake Into Pose, and select Baked Upon (at Start) | Original. Under Root Transform Position (XZ), leave Bake Into Pose unchecked. Click on Apply to confirm the changes.
    How to do it...
  6. Select the Swat@toss_grenade animation clip. Activate the Rig section. Then, change Animation Type to Humanoid, and Avatar Definition to Create From this Model. Confirm it by clicking on Apply.
  7. Now, activate the Animations section. Select the toss_grenade clip (from the Clips list), click on the button Clamp Range to adjust the timeline, and leave the Loop Time and Loop Pose options unchecked. Under Root Transform Rotation, check Bake Into Pose, and select Baked Upon (at Start) | Original. Under Root Transform Position (Y), check Bake Into Pose, and select Baked Upon (at Start) | Original). Under Root Transform Position (XZ), leave Bake Into Pose unchecked. Click on Apply to confirm the changes.
  8. Let's create a Mask. From the Project view, click on the Create button and add an Avatar Mask to the project. Name it as BodyMask.
  9. Select the BodyMask tab and, in the Inspector view, expand the Humanoid section to unselect the character's legs, base, and IK spots, turning their outline red.
    How to do it...
  10. From the Hierarchy view, select the MsLaser character. Then, from the Animator component in the Inspector view, double-click on the MainCharacter controller to open it.
  11. In the Animator view, create a new layer by clicking on the + sign at the top-left Layers tab, above the Base Layer.
  12. Name the new layer as UpperBody and click on the gear icon for the settings. Then, change its Weight to 1, and select the BodyMask in the Mask slot. Also, change Blending to Additive.
    How to do it...
  13. Now, in the Animator view, with the UpperBody layer selected, create three new empty states (by right-clicking on the gridded area and navigating to, from the menu, Create State | Empty). Name the default (orange) state null, and the other two as Fire and Grenade.
  14. Now, access the Parameters tab and add two new parameters of the Boolean type: Fire and Grenade.
    How to do it...
  15. Select the Fire state and, in the Inspector view, add the firing_rifle animation clip to the Motion field.
    How to do it...
  16. Now, select the Grenade state and, in the Inspector view, add the toss_grenade animation clip to the Motion field.
  17. Right-click on the null state box and, from the menu, select Make Transition. Then, drag the white arrow onto the Fire box.
  18. Select the arrow (it will turn blue). From the Inspector view, uncheck the Has Exit Time option. Then, access the Conditions list, click on the + sign to add a new condition, and set it as Fire and true.
    How to do it...
  19. Now, make a transition from null to Grenade. Select the arrow (it will turn blue). From the Inspector view, uncheck the Has Exit Time option. Then, access the Conditions list, click on the + sign to add a new condition, and set it as Grenade and true.
  20. Now, create transitions from Fire to null, and from Grenade to null. Then, select the arrow that goes from Fire to null and, in the Conditions box, select the Fire and false options. Leave the Has Exit Time option checked.
  21. Finally, select the arrow that goes from Grenade to null. In the Conditions box, select the options Grenade, false. Leave the Has Exit Time option checked.
    How to do it...
  22. From the Hierarchy view, select the MsLaser character. Locate, in the Inspector view, the Basic Controller component and open its script.
  23. Immediately before the end of the Update() function, add the following code:
      if(Input.GetKeyDown(KeyCode.F)){
        anim.SetBool("Grenade", true);
      } else {
        anim.SetBool("Grenade", false);
      }
      if(Input.GetButtonDown("Fire1")){
        anim.SetBool("Fire", true);
      }
      if(Input.GetButtonUp("Fire1")){
        anim.SetBool("Fire", false);
      }
  24. Save the script and play your scene. You will be able to trigger the firing_rifle and toss_grenade animations by clicking on the fire button and pressing the F key. Observe how the character's legs still respond to the Move animation state.

How it works...

Once the Avatar mask is created, it can be used as a way of filtering the body parts that would actually play the animation states of a particular layer. In our case, we have constrained our fire_rifle and toss_grenade animation clips to the upper body of our character, leaving the lower body free to play the movement-related animation clips, such as walking, running, and strafing.

There's more...

You might have noticed that the UpperBody layer has a parameter named Blending, which we have set to Additive. This means that animation states in this layer will be added to the ones from the lower layers. If changed to Override, the animation from this would override animation states from the lower layers when played. In our case, Additive helped in keeping the aim stable when firing while running.

For more information on Animation Layers and Avatar Body Masks, check out Unity's documentation at http://docs.unity3d.com/Manual/AnimationLayers.html and http://docs.unity3d.com/Manual/class-AvatarMask.html.

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

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