You are previewing Unity Character Animation with Mecanim.
O'Reilly logo
Unity Character Animation with Mecanim

Book Description

A detailed guide to the complex new animation tools in Unity, packed with clear instructions and illustrated with original content in the context of a next generation zombie apocalypse adventure game

About This Book

  • Create and export models and animation sequences to Unity from 3ds max and Maya

  • Prepare character models and animation for games using Mecanim's rigging tools

  • Retarget, adjust, and mix and match motion capture and other animation data

  • Write and edit scripts compatible with Mecanim Animation Controllers

  • Who This Book Is For

    If you are a Unity developer looking to get to grips with the character animation specific tools, a 3D software user who is new to Unity, or a beginner game developer who is interested in character animation and interaction, this book is ideal for you. Some experience with either the Unity interface or basic 3D coordinates is recommended, but not required.

    What You Will Learn

  • Learn how to prepare a rigged character model to receive animation within Unity

  • Acquire efficient techniques to refine and optimize motion capture data

  • Retarget animation sequences between different character rigs

  • Discover how to rig a humanoid character and export for use in Unity

  • Script character interaction for a First Person character model

  • Create dynamic animation sequences from scratch using keyframe techniques, in a variety of 3D software packages

  • Learn Project Management in Unity

  • Understand how to set up a complex facial rig for speech

  • Set up Animation Controllers with masked states and blend trees to create seamless and additive animation transitions

  • Construct a ragdoll game object and instantiate it in a game

  • Devise Mecanim animation integration for the player and AI driven animation for enemy characters

  • In Detail

    Game animation for independent developers has taken a giant leap forward with Unity's Mecanim toolset, which streamlines the import/export, retargeting, and many other aspects of the character animation workflow. Unity Character Animation with Mecanim is a great primer for getting to know the nuts and bolts of Mecanim and other character animation related tools in Unity. It offers you step-by-step instructions for preparing and exporting rigged models and animation sequences from commonly used 3D packages, such as Maya, 3ds Max and Blender.

    This book explores the new set of animation tools introduced with Mecanim in Unity. Approaching its subject matter through a typical genre-a zombie action game, character animation techniques are explored using real examples of player input and interaction, enemy behavior, and other aspects of game dynamics. As the book progresses, the reader will understand how these elements fit together in a small game development workflow.

    We will begin with a demonstration of the process of getting a rigged character into Unity and setting it up to use provided animation sequences. We will also consider a few industry standard 3D packages and how these can be used to rig a humanoid character for use in Unity. We will demonstrate the retargeting capabilities of Mecanim's Humanoid Animation type by adjusting motion sequences to fit disparate character types in our game. After this, we will look at Ragdoll physics and the implementation of this commonly used technique in a Mecanim workflow. The book culminates with a thorough dissection of the enemy character AI script incorporating the Mecanim elements detailed in the previous chapters.

    Unity Character Animation with Mecanim will provide you with a detailed exploration of the interaction between game development and character animation, and will broaden your understanding of the rich animation toolset within Unity.

    Style and approach

    A comprehensive guide, featuring step-by-step practical tutorials using sample assets, showing you how to build fully controllable characters and non-player characters/enemies.

    Downloading the example code for this book You can download the example code files for all Packt books you have purchased from your account at If you purchased this book elsewhere, you can visit and register to have the files e-mailed directly to you.

    Table of Contents

    1. Unity Character Animation with Mecanim
      1. Table of Contents
      2. Unity Character Animation with Mecanim
      3. Credits
      4. About the Author
      5. About the Reviewers
        1. Support files, eBooks, discount offers, and more
          1. Why subscribe?
          2. Free access for Packt account holders
      7. Preface
        1. The context
        2. What this book covers
        3. What the book does not cover
        4. What you need for this book
        5. Who this book is for
        6. Conventions
        7. Reader feedback
        8. Customer support
          1. Downloading the example code
          2. Downloading the color images of this book
          3. Errata
          4. Piracy
          5. Questions
      8. 1. The Zombie Attacks!
        1. Setting up the project
          1. Importing our enemy
            1. Organizing the material and textures
            2. Adjusting the import scale
            3. Adjusting the rig import settings and creating the Avatar
              1. Choosing the appropriate rig import settings
              2. Creating the Avatar
                1. Adjusting the character's pose
                2. Checking the bone hierarchy in the Avatar
          2. Setting up the animator controller
            1. Creating states
            2. Creating a parameter
            3. Adding transitions
            4. Writing the script
          3. Adjusting the scene elements to preview the animation
            1. Previewing the hit animation in the game view
        2. Summary
      9. 2. Rigging Characters for Unity in 3ds Max and Maya
        1. Understanding the need for rigging
        2. Minimum requirements
        3. Sourcing models
        4. Modeling for animation
        5. Rigging in 3ds Max
          1. Setting up the scene
          2. Importing the character model
          3. Creating the Biped system
          4. Adjusting the Biped's parameters
            1. Adjusting the Biped to fit the character
            2. Switching to X-ray mode and freezing the character mesh
              1. Changing bone display colors
              2. Scaling and orienting the bones to match the character model
                1. Repositioning the joints for the ponytail and jaw
            3. Adjusting limb positions
            4. Renaming bones
            5. Copying and pasting the position, rotation, and scale data from one side to the other
          5. Skinning the character using the skin modifier
            1. Creating a selection set
            2. Saving the default pose
            3. Creating the test pose
            4. Making adjustments to the Skin modifier
              1. Adjusting envelopes
              2. Adjusting influence on the head vertices
              3. Painting skin weights for the jaw bone
          6. Exporting the rigged character
        6. Rigging in Maya
          1. Setting up Maya to rig our character model
            1. Setting system units to meters
            2. Changing the display grid size
            3. Importing the model
            4. Adjusting the model scale
            5. Adjusting the viewport display and toolset for joint creation
            6. Creating joints for the back, neck, and head
            7. Renaming and repositioning the joints
          2. Creating the leg joints
          3. Creating the arm joints
            1. Cloning the finger joints
          4. Mirroring joints for the leg and arm chains
            1. Connecting the arm and leg chains
            2. Creating the ponytail and jaw joints
            3. Aligning joint transforms
            4. Creating a test pose
          5. Binding the character mesh to the skeleton
          6. Painting skin weights
          7. Exporting for unity
        7. Summary
      10. 3. Interacting with the Environment
        1. Importing the project assets package
          1. Setting up the player character
          2. Creating a new scene
          3. Adding the player character model to the scene
          4. Adding the character controller
            1. Adjusting the camera height
          5. Adding the shoot idle animation
            1. Adding and parenting the gun
            2. Saving the first-person rig as a prefab
            3. Adding the office-level scene
            4. Completing the camera setup
            5. Modifying the animator controller
            6. Setting the transition
              1. Creating a trigger parameter for the Shoot state
          6. Writing and implementing the character animation script
          7. Adding the initial code to the FPSAnimation script
          8. Adding the script to the player character game object
        2. Adding and implementing collectable objects
          1. Instantiating the lunchBox collectable in the game level
            1. Inspecting the lunchBox collectable's components
              1. Looking at the collectable script
              2. Implementing self destruction in the collectable script
          2. Setting up the player character's response
            1. Adding the Pickup state to the animator controller
              1. Setting the Pickup state's transitions and parameter
                1. Transitioning between the ShootIdle and Pickup states
                2. Creating and adding the parameter
            2. Creating the pickup camera
            3. Finalizing the pickup camera prefab
            4. Looking at the player status script
              1. Adding the PlayerStatus script to the player character game object
            5. Updating the Collectable script
              1. Hooking up variables in the collectable script
            6. Updating the FPSAnimation script
            7. Modifying the pickup script
          3. Testing the lunchBox collectable
        3. Summary
      11. 4. Working with Motion Capture Data
        1. Introduction to motion capture sequences and their characteristics
        2. Using a motion capture sequence with a pre-rigged model
          1. Getting started
          2. Importing the motion capture sequence
            1. Adjusting the import settings
              1. Adjusting the rig import settings
            2. Adjusting the sequence in the Animations tab
          3. Creating the second walk cycle
            1. Adding the new motion clips to the animation controller
            2. Creating a script to see both animation loops in action
              1. Adding the script and previewing the animation switch
        3. Summary
      12. 5. Retargeting Animation
        1. Loading the scene
          1. Adding and previewing the animation
          2. Adjusting import settings to get a better fit
            1. Creating a duplicate walk cycle
            2. Adjusting the motion parameters
            3. Adjusting the muscle limits
          3. Working with Avatar Body Masks
            1. Opening the new scene
          4. Creating a second layer in the animator controller
            1. Creating states in the mask layer
            2. Setting the parameter and transitions in the mask layer
            3. Editing the script
            4. Previewing the masked animation
        2. Creating five walk variations from two walk cycles
        3. Adding more Avatar masks
        4. Summary
      13. 6. Talking Heads
        1. Adding the snarl face animation to the female zombie character
          1. Setting the scene in Unity
        2. Adding code to the zombie_ready script
          1. Adding the TurnToPlayer function
          2. Connecting the variables in the Inspector panel
        3. Updating the animator controller to include the face animation
          1. Adding the Snarl state
          2. Creating a Null state
          3. Setting transitions between the Null and Snarl states
            1. Creating the IsSnarling parameter
            2. Editing the script to include the Face layer
            3. Smoothing the zombie's turn rotation
        4. Implementing the turn animation
          1. Adding the turning state
          2. Setting up the IsTurning parameter
            1. Creating the transitions to connect the turning state
          3. Setting the transitions for the Turn state
            1. Updating the zombie_ready script to accommodate the Turn state
              1. Creating the Snarl function
              2. Synchronizing the snarl sound
        5. Driving a blendshape animation with the animator controller
          1. Viewing the blendshape in Unity
          2. Keyframing the face blendshape
          3. Updating the animator to handle the blendshape animation
        6. Summary
      14. 7. Controlling Player Animation with Blend Trees
        1. Adding a Blend Tree to the player's existing animator controller
          1. Adding strafing animation to the player character with a Blend Tree
          2. Using Blend Tree properties
          3. Adding the motion clips to the Blend Tree
            1. Adding and adjusting the Blend Tree parameters and thresholds
          4. Updating the character script to use the Blend Tree
          5. Testing the Blend Tree in the Game View
        2. Varying the pickup animation with a Blend Tree
          1. Viewing the pickup_heavy animation sequence
          2. Creating a Blend Tree in the Pickup state
            1. Setting the pickup Blend Tree parameter
            2. Setting the threshold for the pickup Blend Tree
          3. Editing the character animation script to accommodate the pickup Blend Tree
          4. Updating the Collectable script to include a weight variable
          5. Sending the objectWeight variable
          6. Updating the Pick function in the character animation script
          7. Testing the blended animation in the game
            1. Instancing the collectable prefabs
            2. Previewing the blended animation
        3. Summary
      15. 8. Implementing Ragdoll Physics
        1. Introduction to joints in Unity
          1. Creating a test scene
          2. Adding a hinge joint
        2. Creating the ragdoll object
          1. Assigning the material
            1. Generating the initial ragdoll
              1. Assigning bones to the ragdoll list
              2. Assigning mass
              3. Previewing the default ragdoll
            2. Adjusting collision objects
              1. Adjusting the radius of the capsule collider to fit the leg
              2. Adjusting the head's collider
            3. Fine-tuning the character joints
              1. Adjusting the rotational limits of the upper arm
              2. Adjusting the rotational limits of the forearm
            4. Adjusting the rotational limits of the head
              1. Previewing the adjusted ragdoll
            5. Adding a custom joint to the ragdoll
              1. Adding a capsule collider to the ponytail
              2. Adding a Rigidbody component
              3. Adding the character joint
            6. Saving the ragdoll as a prefab
        3. Summary
      16. 9. Controlling Enemy Animation with AI and Triggers
        1. Implementing range detection
          1. Looking at the scene
          2. Adding the initial AI script
          3. Adding proximity detection to the enemy AI script
          4. Setting up the patrol behavior
            1. Adding variables for the patrol
            2. Adding the initial patrol code to the Update function
          5. Defining patrol points
          6. Modifying the animator
          7. Adding and accessing an animation curve
            1. Accessing the animation curve in Mecanim and using it in the script
          8. Adding the attack
            1. Adding the Attack state
            2. Associating tags with the enemy and player game objects
            3. Allowing the zombie to hurt the player
          9. Damaging and killing the zombie
            1. Allowing the player to fire
        2. Pathfinding and obstacle detection with navMesh
          1. Suspending navigation during the turn
          2. Adjusting navigation during the attack
            1. Modifying the Attack function
        3. Timing out the zombie's pursuit
        4. Summary
      17. Index