Configuring a character's Avatar and idle animation

A feature that makes Mecanim so flexible and powerful is the ability of quickly reassigning animation clips from one character to another. This is made possible through the use of Avatars, which are basically a layer between your character's original rig and the Unity's Animator system.

In this recipe, we will learn how to configure an Avatar skeleton on a rigged character.

Getting ready

For this recipe, you will need the [email protected] and Swat@rifle_aiming_idle.fbx files, which are contained inside the 1362_07_code/character_and_clips/ folder.

How to do it...

To configure an Avatar skeleton, follow these steps:

  1. Import the [email protected] and Swat@rifle_aiming_idle.fbx files to your project.
  2. Select from the Project view, the MsLaser@T-Pose model.
  3. In the Inspector view, under MsLaser@T-Pose Import Settings, activate the Rig section. Change Animation Type to Humanoid. Then, leave Avatar Definition as Create From this Model. Finally, click on the Configure… button.
    How to do it...
  4. Inspector view will show the newly created Avatar. Observe how Unity correctly mapped the bones of our character into its structure, assigning, for instance, the mixamoRig:LeftForeArm bone as the Avatar's Lower Arm. We could, of course, reassign bones if needed. For now, just click on the Done button to close the view.
    How to do it...
  5. Now that we have our Avatar ready, let's configure our animation for the Idle state. From the Project view, select the Swat@rifle_aiming_idle file.
  6. Activate the Rig section, change Animation Type to Humanoid and Avatar Definition to Create From This Model. Confirm by clicking on Apply.
    How to do it...
  7. Activate the Animations section (to the right of the Rig). Select the rifle_aiming_idle clip (from the Clips list). The Preview area (at the bottom of the Inspector) will display the message as No model is available for preview. Please drag a model into this Preview area. Drag MsLaser@T-Pose to the Preview area to correct this.
    How to do it...
  8. With rifle_aiming_idle selected from the Clips list, check the Loop Time and Loop Pose options. Also, click on the Clamp Range button to adjust the timeline to the actual time of the animation clip. Then, 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, and select Baked Upon (at Start) | Center of Mass. Finally, click on Apply to confirm the changes.
    How to do it...
  9. In order to access animation clips and play them, we need to create a controller. Do this by clicking on the Create button from the Project view, and then selecting the Animator Controller option. Name it as MainCharacter.
  10. Double-click on the Animator Controller to open the Animator view.
  11. From the Animator view, right-click on the grid to open a context menu. Then, select the Create State | Empty option. A new box named New State will appear. It will be in orange, indicating that it is the default state.
    How to do it...
  12. Select New State and, in the Inspector view, change its name to Idle. Also, in the Motion field, choose rifle_aiming_idle by either selecting it from the list or dragging it from the Project view.
    How to do it...
  13. Drag the MsLaser@T-Pose model from the Project view into the Hierarchy view and place it on the scene.
  14. Select MsLaser@T-Pose from the Hierarchy view and observe its Animator component in the Inspector view. Then, assign the newly created MainCharacter controller to its Controller field.
    How to do it...
  15. Play your scene to see the character correctly animated.

How it works...

Preparing our character for animation took many steps. First, we created its Avatar, based on the character model's original bone structure. Then, we set up the animation clip (which, as the character mesh, is stored in a .fbx file), using its own Avatar. After this, we adjusted the animation clip, clamping its size and making it a loop. We also baked its Root Transform Rotation to obey the original file's orientation. Finally, an Animator Controller was created, and the edited animation clip was made into its default Animation state.

The concept of the Avatar is what makes Mecanim so flexible. Once you have a Controller, you can apply it to other humanoid characters, as long as they have an Avatar body mask. If you want to try it yourself, import mascot.fbx, which is also available inside the charater_and_clips folder, apply steps 3 and 4 into this character, place it on the scene, and apply MainCharacter as its Controller in the Animator component. Then, play the scene to see the mascot playing the rifle_aiming_idle animation clip.

There's more...

To read more information about the Animator Controller, check out Unity's documentation at http://docs.unity3d.com/Manual/class-AnimatorController.html.

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

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