This chapter explains the essentials of working with animations in Unity and covers the following topics:
Unity is a great game engine that implements the animate everything philosophy, which allows you to visualize even the most creative gameplay ideas. Through the course of this book, we will learn a variety of recipes that will help you unleash the power of Unity 5.x animation tools and make your games more fun.
In this first chapter, we will get more familiar with using skeletal animations in Unity. It will guide you through the process of importing such animations, editing them, and assigning them to your in game characters. This knowledge is essential for understanding recipes described in further chapters.
This first recipe shows how to import a skeletal animation from a 3D package. It assumes that you have an animation already prepared. We are going to bring the file to Unity and show where you can adjust the import settings. We will not jump into too much detail for now to make the import process as quick as possible. We will cover all the settings in further recipes.
Most animations are created in external 3D packages such as Maya, 3ds Max, Motion Builder, or Blender 3D. Make sure you have prepared a 3D model with a skeleton, the model is skinned, and you have created at least one animation. You can also download the provided example; open the project in Unity and go to this folder: Chapter 01 Working with animations\Recipe 01 Importing skeletal animations\Sheep Model
. You will find an imported FBX file called Sheep.fbx
there.
To import a skeletal animation, you need to follow these steps:
If you are using Blender, you don't have to manually export the file to FBX format. Simply drag and drop the file to your Assets
folder. Unity will use Blender FBX exporter in the background. Additionally, you will be able to open the file by double-clicking on it in Unity. After you edit and save it, Unity will reimport it again. It is a very convenient way of editing animations as you don't have to go through the export-import process each time you make a change.
Assets
folder in your project or go to Assets
| Import new asset
and choose your FBX file.Inspector
tab. Click on the Rig
button:Humanoid
option, for all the rest choose Generic
. We use a sheep model in this example, so we need to choose the Generic
rig. Differences between those options are described in the How it works section.Animations
button. If an Unapplied import settings
window appears, click on Apply
.Unity uses four different rig configurations:
Generic
rig: This one is used for quadrupeds, spiders, and other non-humanoid characters. It uses Unity's Mecanim system for controlling animation flow. It can use root motion if you provide a Root node—
a bone responsible for character movement.Humanoid
rig: This one is used for humanoid characters only. It also uses Mecanim system for controlling animation. If you are using Humanoid
rigs, you have more options for importing animations. The biggest advantage is automatic animation retargeting—you can use the same animation clip on various types of humanoid characters (for example, a dwarf and a troll).Legacy
: This option is used mostly for backward compatibility with previous versions of Unity. It is similar to the Generic
rig but uses scripts instead of Mecanim for controlling animation flow. I recommend using Generic
rig instead.None
: This last option turns off the rig and disables animation import.We will discuss differences between Generic
and Humanoid
rigs further in the next recipe.
Clips
section of the Animations
tab.Clips
section. You can also remove clips by clicking on the minus button.Source Take
drop-down menu.Source Take
drop-down menu.Start
and End
input fields.If you are using Blender, make sure to rotate the rig -90 degrees in the X axis, apply the rotation in Blender and then rotate it again, +90 degrees in the X axis. The rotation of the rig in Blender should be: 90 X, 0 Y, and 0 Z. Your model should be facing the -Y axis in Blender (when you change the view to FRONT
, you should see the face of your character). Blender and Unity use different axes alignment: in Blender, Z is up and Y is back; in Unity Z, is front and Y is up. This little trick solves the problem. Your model after import should have 0 rotation and should be facing the Z axis. It is important for moving objects (such as characters) and rig configuration. If you don't do this, your model will have -90 X rotation after the import.
This recipe shows how to configure two most frequently used rig types: Generic
and Humanoid
. We will go through all the available options for both of them.
As previously mentioned, make sure you have prepared two animated characters. One of them should be a humanoid and the other a non-humanoid, a quadruped for instance. You can also download the provided example Unity project and go to the Chapter 01 Working with animations\Recipe 02 Configuring generic and humanoid rigs\Rigs
directory.
You will find there three FBX files:
Generic.fbx
Humanoid.fbx
Quadruped.fbx
If you are creating your characters from scratch, the bones hierarchy of your humanoid rig is important for Unity to recognize the rig as a humanoid. It should follow this pattern:
HIPS -> SPINE -> CHEST -> NECK -> HEAD HEAD -> EYE (for left and right eyes) HEAD -> JAW HIPS -> UPPER LEG -> LOWER LEG -> FOOT -> TOES (for left and right legs) CHEST -> SHOULDER -> ARM -> FOREARM -> HAND (for left and right hands) HAND -> PROXIMAL -> INTERMEDIATE -> DISTAL (for five fingers in left and right hands)
The hip bone is the root bone of the humanoid character. Fingers, shoulders, chest, neck, eyes, jaw, and toes are optional bones. Your humanoid character will work without them.
You should also remember to model the character in a T-POSE. It should face the Z axis in Unity (if your 3D software uses different axis alignment from Unity's like Blender, remember about the -90 and +90 degrees rotation trick shown in the Importing skeletal animations recipe). Hands of the character should be flat, palm down, and parallel to the ground along the X axis. A-POSE characters will also work.
To configure a generic rig you need to follow these steps:
Rig
tab in the Inspector
. Then select the Generic
option in the Animation Tab
drop-down menu. If you are using the provided example Unity Project, select the Generic.fbx
file in the Chapter 01 Working with animations\Recipe 02 Configuring generic and humanoid rigs\Rigs
directory.Create From This Model
option from the Avatar Definition
drop-down menu.Root Node
option set to None
.Optimize Game Objects
option unchecked.Apply
button to complete the configuration.Humanoid rig configuration has a lot more options:
Rig
tab in the Inspector
. Then select the Humanoid
option in the Animation Tab
drop-down menu. If you are using the provided example Unity Project, select the Humanoid.fbx
file in the Chapter 01 Working with animations\Recipe 02 Configuring generic and humanoid rigs\Rigs
directory.Create From This Model
option from the Avatar Definition
drop-down menu.Optimize Game Objects
option unchecked.Apply
button.Configure
button with a tick icon near to it.Humanoid
for the Generic.fbx
file.
Some quadruped characters can be mistaken by Unity for humanoids. You can find a Quadruped.fbx
file in the provided example Unity project. Theoretically, you can set its rig to Humanoid
, and Unity will recognize it as valid. This, however, is a mistake—all quadruped characters' rigs should be set to Generic
. Setting them as humanoids can cause problems later.
Mapping
section (1). All recognized and assigned bones are shown as green body parts on the displayed dummy character. If a required bone is missing or is not assigned, it will show up as red. You can navigate between the Body
, Head
, Left Hand
, and Right Hand
sections by clicking on a corresponding button (2). All bones are displayed in a list (3) for each section. Required bones are marked with circle icons and optional bones are marked with dotted circle icons.Hierarchy
tab and dropping it onto a corresponding bone slot in the Inspector
tab. Unity will occasionally miss a bone or two (especially fingers); thus, you should always check the bone assignment manually.Character is not in T-POSE
message will be displayed in the scene view and the character's bones will show up in red. Unity needs the character in a T-POSE for proper humanoid avatar configuration. You can enforce that pose by choosing the Pose
|Enforce T-POSE
option, found below the bones mapping
list.Apply
button and then the Done
button to finish configuration.
Humanoid
rig uses more advanced Mecanim features than the Generic
rig. You can find the list of such features below:
Humanoid
rig uses automatic retargeting, which means that you can have the same animations on different humanoid characters. Your characters can share a group of animations (basic movement or some common actions). It gives you the possibility to buy your animation assets from the Asset Store and easily use them on your characters. Generic
rigs don't support this feature. You have to prepare animations for your specific rig or retarget them in a 3D package.Generic
rigs have to use custom-made solutions.mirror
option. We will discuss them in depth in the Looping mirroring and offsetting animations recipe.Humanoid
rigs. It is not true. If your rig has an animated weapon slot for instance, you can still use it with the Humanoid
rig. All you need to do is to find the Mask
section in your animation import settings and enable the additional bone for each animation it is used in (you need to use the Transform
foldout to find your additional bone).I highly recommend using Humanoid
rigs for all humanoid characters in your game.
Muscle Group Preview
section by adjusting the sliders (1). You can preview the range of movement per muscle in the Per-Muscle Settings
section. You can also adjust the range here by unfolding a given muscle foldout and using the slider (2). In the Additional Settings
section, you can adjust more options of your avatar. These are mainly responsible for the flexibility of your rig. You can set how much a bone can be stretched during animation for instance.Translation DoF
option enables animating bones transition in your Humanoid
rig. It is turned off by default, meaning that only rotation of the bones is used in animation.Rig
tab in the model import settings
, you can find some additional options:
Avatar Definition
: This option is responsible for creating a new avatar or copying the avatar from another model. The second option is useful for storing animations in multiple files. It will be discussed further in the Using animations from multiple assets recipe.Optimize Game Objects
: This option lets you hide all bones from the Hierarchy
view. The number of game objects in the game has an impact on the performance. You can still choose a number of bones that will be displayed in the Hierarch
y. It is useful for having exposing weapon slots and similar gameplay-related bones while hiding others.Root node
: This option is visible only for Generic
rigs. It lets you choose the bone responsible for root motion calculation. We will discuss it further in Chapter 4, Character Movement.Animator Controllers are state machines (graphs) responsible for controlling the flow of animations of any animated object in the game. The same Animator Controller asset can be used by multiple objects or characters. Unity will create an independent runtime copy of the asset for each animated object it is assigned to.
As always, you should have a rigged and animated character ready before we start. Import it into Unity, choose the proper rig type, and put it into a scene. You can download the example Unity project and go to the Chapter 01 Working with animations\Recipe 03 Creating and assigning an animator controller
directory. There is a scene called Example.unity
there. If you open it, you'll find a Sheep character in the Hierarchy
. It has an Animator Controller already created and assigned. You can also use the Quadruped.fbx
file from the Chapter 01 Working with animations\Recipe 03 Creating and assigning an animator controller\Rigs
directory to follow the recipe step by step.
To create and assign an Animator Controller, follow these steps:
Project View
(any directory in the Assets
folder) and press the right mouse button.Create
| Animator Controller
from the menu. A controller asset will be created. You can name it as you wish.Animator
tab will appear. It will show the current selected Animator Controller
.Project View
. Unfold it and drag and drop one of the imported animations into the Animator
window. A new state will be created and will be colored orange, showing that this is the default animation state—the state from which your graph starts.Inspector
tab. All animated objects have an Animator
component added automatically.Controller
slot in the Animator
component inspector.Animator Controller
asset into the Controller
slot of the Animator
component.Animator
tab to see what animation state the character is currently in. Current animation state will have a blue progress bar displayed.Every animated object in Unity uses an Animator component and an Animator Controller asset. The component is responsible for playing animations in runtime. It has a number of parameters that we have to set or we can use to tweak the component's functionality:
Avatar
: In Unity, Avatars
are rig definitions. For instance, if we have multiple files containing animations with the same Generic
rig, we should use the same Avatar
for all of them. You can find more information about it in the Using animations from multiple assets recipe.Apply Root Motion
: With this checkbox, we can turn the root motion on and off. It can be useful when we have animations with root motion but don't want to use the root motion definition for a given character.Update Mode
: This parameter tells Unity in which update the animations should be evaluated. The Normal
option makes the animations synchronized with the normal Update()
call, the Animate Physics
option synchronizes animations with the physics FixedUpdate()
call, and the Unscaled Time
option synchronizes the animation with the normal Update()
call, but disables animation time scaling (the animation is played with 100 percent speed regardless of the Time.timeScale
variable value).Culling Mode
: This parameter tells Unity when to turn off the animation playback on a given Animator. The Always Animate
option makes the Animator always play animations (event when off-screen), the Cull Update Transforms
option culls Retarget and IK Transforms when the Animator is not visible on screen, and the Cull Completely
option disables the animation completely when the Animator is not visible on screen.The Animator Controller asset stores a graph of animations (animation states) and defines the rules of switching between them, blending them, and so on. The controller (asset) is attached to the component's Controller
field (the component is attached to a character prefab or a character placed in the scene). Many objects or characters can share the same Animator Controller if they use the same animations (have the same rigs or are humanoid characters).
As mentioned previously, Animator Controllers are state machines containing animations (states). Any given character can be in one such state (play one animation) at a time. To switch between states (animations), you need to create state transitions.
Before we start, you should have an animated model placed on a scene with an Animator Controller assigned. You can find such a model in the provided example Unity project. Go to the Chapter 01 Working with animations\Recipe 04 Creating animation transitions in Animator Controller
directory. Open the Example.unity
file. You will find a Sheep
object in the Hierarchy
. It has an Animator Controller
assigned. You can open it by double-clicking on the Controller
field in the Animator
component of the Sheep
game object.
To create a state transition in an Animator Controller, follow these steps:
Animator Controller
asset.Animator
window. You can also point the cursor at an empty space in the Animator
window, press the right mouse button and select Create State
| Empty
, then select the state, and add an animation to the Motion
field in the Inspector
.Make Transition
.State transitions define how we can travel through the graph of animations. They are combined with transition conditions based on Animator Controller parameters; we will discuss the parameters in detail in the next recipe: Using parameters to control the animation flow. Each transition has a set of properties we can adjust. Click on the transition (white arrow) to select it. You can find the transition properties in the Inspector
tab (make sure to unfold the Settings
foldout):
Has Exit Time
: If set to true, this enables the transition only after a given percentage of the animation has been already played. If disabled, the transition will take place instantly after its conditions are met. If you want to create a sequence of animations, set it to true.Exit Time
: This is the percentage (0-1 range) of the animation length after which the Has Exit Time
condition is met. If you set it to 0.5
, for instance, the state will transition after 50 percent of the animation was played.Fixed Duration
: This property is combined with the next one, Transition Duration (s)
. If it's on, the Transition Duration (s)
is in seconds, and if it's off, the Transition Duration (s)
is in percentage of the animation time.Transition Duration (s)
: the time of the state transition. This is how long the animations will transition one into another. Animations are being blended together during the transition. Longer transitions are smoother, shorter, are more sharp. A good default value is around 0.25
.Transition Offset
: This offsets the target animation in time. The value is in percentage of the animation. Setting it to 0.3
means that the target animation will start from 30 percent of its length instead of the beginning.Interruption Source
: This setting tells Unity whether the transition can be interrupted by other transitions. You can set it to:
None:
the transition cannot be interrupted by any other transition.Current State:
The transition can be interrupted by transitions from the state we are trying to transition from.Next State:
The transition can be interrupted by transition from the state we are trying to transition to.Current State then Next State:
The transition can be interrupted by the transitions of the state we are trying to transition from or by the transitions of the state we are trying to transition to. The transitions from the state we are trying to transition from take the priority.Next State then Current State:
The transition can be interrupted by the transitions of the state we are trying to transition from or by the transitions of the state we are trying to transition to. The transitions from the state we are trying to transition to take the priority.Ordered Interruption:
If set to false, this lets the transition be interrupted by other transitions independently of their order. If set to true,the order of the transitions matters.When you create an Animator Controller, you can see three more nodes apart from your animation states:
Entry and Exit
: This node is used when you transition between state machines (Animator Controllers or substate machines). We will discuss it in detail in the Using Sub-State Machines in Animator Controller recipe in Chapter 6, Handling Combat.Any state
: This node can be used as a helper to make a transition from any state to a given state. It is used when you have an animation that can be played anytime, for instance, a hit animation. We will discuss it in detail in the Using transitions from any state to play hit reactions recipe in Chapter 6, Handling Combat.You can define a set of parameters in an Animator Controller and use them to control the transitions between animation states in Mecanim. In this recipe, we will show how to use parameters for transition conditions and use scripts to set values of those parameters in runtime.
Before we start, you should prepare an Animator Controller with at least one transition between animation states. The controller should be assigned to a character (its Animator component) placed in a scene. You can also use the provided example Unity project and go to the Chapter 01 Working with animations\Recipe 05 Using parameters to control the animation flow
directory. You will find an Example.unity
scene there. There is a Warrior
game object in the scene's Hierarchy
. If you run the game and press the space bar, the Warrior
will make a wave gesture. You can select the Warrior
and open his Animator Controller
. If you click on the Idle
| Wave
transition, you will be able to see the transition condition.
To use parameters for controlling state transitions, follow these steps:
Animator Controller
asset.Parameters
tab in the upper left corner of the Animator
window and click on it.Parameters
tab to add a new parameter.Trigger
type for the parameter.Wave
).Idle
and Wave
animation states. Idle
is the default state.Inspector
tab and find the Conditions
section.Has Exit Time
option, found above the Settings
foldout.Project View
and select Create
| C# Script
. Name the script as you wish (in the provided example, it's called Wave
, the same as the parameter it sets).using UnityEngine; using System.Collections; public class Wave : MonoBehaviour { //The anim variable is used to store the reference //to the Animator component of the character. private Animator anim; void Start () { //We get the component and assign it to //the anim variable when the game starts anim = GetComponent<Animator>(); } void Update () { //We check if player pressed the spacebar if (Input.GetKeyDown(KeyCode.Space)) { /*We cal the SetTrigger() function on the Animator component stored in the anim variable. The function requires one parameter - the name of the trigger parameter set in our Animator Controller ("Wave" in our example). Make sure to match it with the name of the parameter you've created in your Animator Controller*/ anim.SetTrigger("Wave"); } } }
Transform
that has the Animator
component with your Animator Controller
attached. Play the game and press the space bar; you should see your character switch to the next animation state.You can use several types of parameters and corresponding script functions to set them:
SetTrigger(string name)
function called on the Animator
component object. It is reset by the Animator Controller
after it is consumed (used) by a transition. The string name
parameter of the function has to match your trigger parameter name set in the Animator Controller
.Equal
, Greater
, Less
, and Not Equal
options to compare the value of your parameter with the given number. Integer type parameters are set with the SetInteger(string name, int value)
function. The string name
parameter needs to match the parameter name set in the controller. The int value
parameter is the value to set the controller parameter to.SetFloat(string name, float value)
function.SetBool(string name, bool value)
function.Conditions
section in the transition Inspector
. For the transition to occur, all its conditions have to be met. It works as logical AND for the conditions.Make Transition
option, and then select the state you already have a transition to. A multi-transition is marked with three arrows instead of one. If the conditions of any of the transitions are met, the transition will occur. You can use it as logical OR for transition conditions.Transitions
section of the Inspector
.Inspector
and click on the minus icon, or select it in the Animator Controller
and press Delete on the keyboard. Pressing Delete removes all the transitions.Inspector
tab and click on the minus icon. To select a condition, you need to click on the handle to the left of the condition drop-down list (the handle looks like a = sign).Animator Controller
, you need to click on the handle on the left of the parameter and press the Delete button on the keyboard. You can also right-click on the parameter and choose the Delete
option from the context menu.At times it is very convenient to separate animations to multiple assets. A common scenario is to have rigged models separated from animations or different animation types separated from each other (combat from movement, movement from actions, actions from cut scenes, and so on). This recipe shows how to do it for both Humanoid
and Generic
rigs.
Before we start, you should prepare and import at least two assets containing animations. If you are using a Generic
rig, make sure to have the same rig in both assets. You can also use the provided example Unity project and go to the Chapter 01 Working with animations\Recipe 06 Using animations from multiple assets
. There is a scene called Example.unity
file there. In the scene Hierarchy
you can find a Character
game object. It has an attached Animator Controller
in which you can find two animations: Idle
and Wave
. In the Rigs
directory, you will find the Character.fbx
asset containing only a rigged character along with the Idle.fbx
and Wave.fbx
assets containing the corresponding animations.
To use animations from multiple assets, you need to follow these steps:
Avatar Definition
to Create From This Model
on your character (or reference character if you plan to have multiple characters with the same rig). For each imported animation asset, set the Avatar Definition
to Copy From Other Avatar
and choose the avatar of your character or reference character. To do so, you need to unfold the character asset, find the avatar, and drag and drop it to the Source
field in the Inspector
tab.Humanoid
rig, you don't have to copy the Avatar Definition
(you still can if your rigs are exactly the same; that way you will have fewer avatars in your project, making it easier to find the ones you need).Animator Controller
.All Generic
rigs can use animations from multiple assets that share the same Avatar Definition
. To share the same Avatar Definition
, Generic
rigs have to have exactly the same rigs (the same hierarchy, the same bone names and transforms). All Humanoid
rigs can use animations from multiple assets that are also set to Humanoid
. Additionally, they don't need to have the same rigs because Unity automatically retargets all humanoid animations.
Unity allows editing the animations to some extent after they're imported. It can save a lot of work and greatly speed up your workflow.
Before we start, you should prepare and import a Humanoid
rig with at least one animation. You can also use the provided example Unity project and go to the Chapter 01 Working with animations\Recipe 07 Looping mirroring and offsetting animations
directory. There is a scene called Example.file
there. In the scene Hierarchy
, you can find a Mirror
game object. It has an attached Animator Controller
in which you can find two animations: Wave
and WaveMirror
. In the Rigs
directory, you will find the Mirror.fbx
asset. If you select it and go to the Inspector
, and to the Animations
tab, you can find normal and mirrored animation examples, as well as looped animation examples (Idle
and IdleMirror
).
To set an animation to loop, you need to go through the following steps:
Animations
tab.Loop Time
checkbox and click on the Apply
button. The animation is looped.Loop Pose
checkbox. It is not recommended to use this option for animations that have matching first and last frames.To offset an animation, you need to go through the following steps:
Animations
tab.Loop Time
checkbox).Cycle Offset
field, below the Loop Pose
checkbox.Apply
button.To mirror an animation, you need to go through the following steps:
Humanoid
asset and go to the Animations
tab.Mirror
checkbox on the bottom of the animation settings.Mirror
checkbox and click on the Apply
button. The animation is mirrored.Humanoid
rigs.Mirror
option for steering animations (clips used to turn the character while moving). We will be showing that in the Using root motion to steer a character recipe in Chapter 4, Character Movement.Humanoid
rigs. It is used to flip the animation left to right and can save up to 50 percent of steering animations when combined with the Offset Cycle
option.You can also mirror and offset animation states in the Animator Controller
. If you select an animation state and go to the Inspector
tab, you can find the Mirror
and Cycle Offset
options. There is also an option to use Animator Controller
parameters to switch the Mirror
option on and off and set the Cycle Offset
. You need to have a Boolean parameter defined for the Mirror
option and a float parameter for the Cycle Offset
. Those settings will be automatically synchronized with the parameters. Whenever you change a parameter value, the setting will also be changed.
Unity allows you to slow down and speed the animation playback in the Animator Controller
. You can do it in runtime with scripts to achieve interesting effects, for instance, slow motion.
Before we start, you should prepare and import a rig with at least one animation and create an Animator Controller
with at least one animation state for it. You can also use the provided example Unity project and go to the Chapter 01 Working with animations\Recipe 08 Adjusting the playback speed of animations
directory. There is a scene called Example.unity
there. In the scene Hierarchy
, you can find and AdjustSpeed
game object. It has an attached Animator Controller
in which you can find two animation states: WaveSpeedNormal
and WaveSpeedIncreased
. There is also an AdjustSpeedByScript
game object in the scene. You can increase the playback speed of its animations by pressing the Space
button on your keyboard in runtime.
To change the animation playback speed, follow these steps:
Animator Controller
.Speed
parameter below the Motion
field.Speed
parameter to change the playback speed.The Speed
parameter set for an animation state in the Animator Controller
multiplies the speed playback of the animation state. Setting this parameter to zero will freeze the animation.
You can also set the parameter using scripts. Following is an example script (it is used by the AdjustSpeedByScript
game object in the provided Example.unity
). You can assign it to your animated game object that has the Animator
component and an Animator Controller
attached:
using UnityEngine; using System.Collections; public class AdjustSpeedByScript : MonoBehaviour { //This is a variable, in which we store the reference to the Animator component private Animator anim; //We store the wanted animation speed in this variable, the default value is 2 (200%). public float newAnimationSpeed = 2f; void Start () { //At the start of the game we assign the Animator component to our anim variable anim = GetComponent<Animator>(); } void Update () { //We check if player pressed the Space button if (Input.GetKeyDown(KeyCode.Space)) { //And set the playback speed of the whole Animator Controller (it multiplies all states animation playback speed) anim.speed = newAnimationSpeed; } } }
If you want to change the speed of just one animation state, then add a float parameter to your Animator Controller
, use this parameter in the Multiplier
field in the animation state Inspector
, and change the parameter with scripts using the following function:
anim.SetFloat(string name, float value);
Here name
is the name of your parameter in the Animator Controller
and value
is the float value you want to set the parameter and playback speed to.
If you have multiple types of character in your game, most probably you would like to be able to share the animation states logic between them and just replace the animation clips. Imagine that you have several types of enemies, and their combat logic is the same (they have attacks, movement, hit reactions, and so on) but they use different animation clips. For such situations, Override Animator Controllers
come in handy.
You should have at least two characters with different animation clips ready and imported into Unity. You can also download the provided example Unity project and go to the Chapter 01 Working with animations\Recipe 09 Using override animator controllers to animate different types of characters
directory. There is a scene called Example.unity
there. If you open it, you'll find Warrior
and Spider
game objects in the Hierarchy
. They have Override Animator Controllers
attached, and you can examine them. If you run the game, the characters will play attack animations. The underlying logic is defined in the HumanCombat
controller (found in the Animator Controllers
directory). The Warrior
game object uses the HumanCombat
controller without overriding it, the Spider
game object uses a SpiderCombat
override controller.
To use Override Animator Controllers
, follow these steps:
Animator Controller
that will be used as the reference controller containing the logic of animation states. In the provided example, it is the HumanCombat
controller, created with Warrior
animations.Animator
component) and use it as previously.Override Animator Controller
by right-clicking on the Project View
and choosing Create
| Override Animator Controller
.Inspector
tab.Animator Controller
to the Controller
field of the newly created override controller.Human
animations are replaced with Spider
animations.Override Animator Controller
to the Controller
field of the Animator
component of your second character.Override Animator Controller
only replace animation clips from your original Animator Controller
. The logic of the original controller stays the same (so you can also use the same scripts to set the same parameters and so on). It is extremely useful for creating NPC characters in your games. You create the Animator Controller
once, you write the scripts driving the controller once and only change the animations.
In Unity, you can import not only skeletal animation but also object transform animation. It can be useful for creating complex movements, for instance, an object following a path.
You have to animate an object's translation, rotation, or scale in a 3D package, then export the object as FBX file. You can also download the provided example Unity project and go to the Chapter 01 Working with animations\Recipe 10 Importing object animation from a 3D package
directory. There is a scene called Example.unity
there. This object has a follow path animation created in Blender and exported to FBX file. It has a normal Animator Controller with that animation as default state (looped). You can run the game to see the object animate.
To import object animation, follow these steps:
Generic
rig type.Animator Controller
and drag and drop one of the imported animations into the Animator
window to create a default animation state.Animator
component of the object.Unity imports translation, rotation, and scale animation of 3D objects from a 3D package. It makes it easy to create complex animations and use advanced features of a chosen 3D software. Make sure to bake your animations into frames before importing them to Unity (Blender bakes the exported animations to frames by default). Importing mesh animation (vertices movement) is not supported (you have to use Blend Shapes
instead; we will discuss them in detail in the Animating facial expressions with Blend Shapes recipe in Chapter 5, Character Actions and Expressions).
Where there is an eBook version of a title available, you can buy it from the book details for that title. Add either the standalone eBook or the eBook and print book bundle to your shopping cart. Your eBook will show in your cart as a product on its own. After completing checkout and payment in the normal way, you will receive your receipt on the screen containing a link to a personalised PDF download file. This link will remain active for 30 days. You can download backup copies of the file by logging in to your account at any time.
If you already have Adobe reader installed, then clicking on the link will download and open the PDF file directly. If you don't, then save the PDF file on your machine and download the Reader to view it.
Please Note: Packt eBooks are non-returnable and non-refundable.
Packt eBook and Licensing When you buy an eBook from Packt Publishing, completing your purchase means you accept the terms of our licence agreement. Please read the full text of the agreement. In it we have tried to balance the need for the ebook to be usable for you the reader with our needs to protect the rights of us as Publishers and of our authors. In summary, the agreement says:
If you want to purchase a video course, eBook or Bundle (Print+eBook) please follow below steps:
Our eBooks are currently available in a variety of formats such as PDF and ePubs. In the future, this may well change with trends and development in technology, but please note that our PDFs are not Adobe eBook Reader format, which has greater restrictions on security.
You will need to use Adobe Reader v9 or later in order to read Packt's PDF eBooks.
Packt eBooks are a complete electronic version of the print edition, available in PDF and ePub formats. Every piece of content down to the page numbering is the same. Because we save the costs of printing and shipping the book to you, we are able to offer eBooks at a lower cost than print editions.
When you have purchased an eBook, simply login to your account and click on the link in Your Download Area. We recommend you saving the file to your hard drive before opening it.
For optimal viewing of our eBooks, we recommend you download and install the free Adobe Reader version 9.