Important notice for Mixamo customers.
We’re retiring features and services on this site, learn how this affects you.
Good character animation can be difficult to achieve but Mixamo and Unity's Mecanim system work perfectly together! Still, it helps to know the basics so we've compiled some general terminology and information about using Unity's Mecanim system. If you're new to character animation in Unity, this is a great place to start!
What is Mecanim?
Mecanim is Unity's animation state machine system. It essentially allows you to create 'states' that play animations and define transition logic that will tell your game how to move between the different states.
When can I use the Mecanim system?
You can use Mecanim if you have an imported .fbx character with a rig. Your mesh and rig can be any type (human, dog, tree, waving flag). Your rig will need to be defined as humanoid or generic as Mecanim does not work with legacy imported characters and animations.
What's the difference between humanoid and generic?
If you import a 2 armed, 2 legged, 1 head character you can define it as a "Humanoid" rig. Humanoid animations can be played across any other rig that is also defined as a humanoid, even if the rig is different.
If it's not a humanoid you'll need to import it as generic. Generic animations can only be played on other identical generic rigs.
The Animator is a special type of component that is attached to animated characters. This is automatically attached to any prefab created from an .fbx imported as humanoid or generic. The Animator is what actually passes information to your game object to control animation. It has several properties:
- Animator Controller The animator controller is a node-based system that defines state machines and handles the transitions of your animations.
- Avatar The avatar is essentially a definition for your rig. You can use it to retarget animations that have the same rig (Super useful for Mixamo animations!). When you import an .fbx an avatar will automatically be generated for it.
- Apply Root Motion With this option you can tell Mecanim to use the root motion of your character to move your character through the scene. If you want to control your character motion through code, you can uncheck this, but it may create less realistic animation and 'footslide' if the animation and speed don't match.
The animator controller is a requirement of the Animator component. It refers to the actual system of creating mecanim animation states and transitions. You can view the animator controller by selecting the asset in the project view or selecting a character that the controller is assigned to and opening the Window > Animator panel. Here is where you will create your states and logic!
A state within Mecanim is a single node and can come in several forms. Clicking on a state will provide useful information like the motion or blendtree used by the state, the speed of the state, whether it is Mirrored or not, and a list of all the transitions from that state to any other state.
There are several types of states you can create:
- Animations You may have a state that is a single animation. To create a single animation state just drag a .anim onto the workspace or right-click and "create new state". Single animation states have parameters such as motion (the animation you're going to use), speed (the rate of playback), and Mirror (flips the motion).
- Blendspaces You may want to control multiple animations without having each one as it's own node. You can do this in a blendspace. Blendspaces can be one dimensional or two dimensional, meaning that they are driven from one or two parameters. A great example is a basic locomotion. You could have horizontal and vertical speed that control forward, backward, strafing, and 45 degree running animations.
- Sub-Statemachines You may have multiple states that are independant of eachother but have their own logic and sub-statemachines are perfect for this. A great example is basic locomotion and actions and then a stealth mode. The stealth mode could be placed into a sub-state machine and have it's own logic, actions and controls.
- Default State Every animator controller should have a default state (displayed in orange). This tells the game where to begin when the game starts.
- Any State This is a very useful state. A transition FROM this state will act as a transition from any and all states. This is useful if you have an animation that should interrupt anything else that is going on. For example, if you have npc's in an FPS animating but when shot should have a dramatic death, you can transition to the death from the Any State state.
- Entry (Unity5) If you're using sub-statemachines the Entry state tells you when your animations are coming from state outside of the current state machine.
- Exit (Unity5) Similarly, the Exit state tells you when your animations are going to a state outside of the current state machine.
In order to move from one state to another you need to create a transition. Transitions can be created by right clicking on a state and choosing "Create Transition". A connector arrow will appear. Drag the connector to the state you want to transition into. Transitions between states are based on conditions and a transition can have one or multiple conditions, shown as a list in the Inspector.
Some conditions include:
- Exit Time (Unity 4) The exit time is a percentage of the animation that you want to complete before automatically transitioning to the next animation. For example, you can transition to Animation A, tell the animation to complete 90% (0.90 exit time) and then automatically transition to Animation B.
- Parameters You can also base your transitions on parameters such as booleans and floats. An example for a boolean could be to transition from a normal state to a sneaking state if the bool "sneaking" is true and to transition back to the normal state if the bool "sneaking" is false.
- Has Exit Time (Unity 5) In Unity 5, Mecanim is a little different. Instead of using Exit time as a parameter it has instead "Has Exit Time". If "Has Exit Time" is true AND no parameter based conditionals are set, then the animation will transition after exit time. If "Has Exit Time" is true and parameter based conditionals are set, Mecanim will wait for the conditionals to become true, then continue playing until the exit time is complete, and then transition. If you want to have your animation transition instantly leave "Has Exit Time" to false and use a parameter based conditional.
To create parameters, open the animator controller. On the right hand panel you'll find the Parameters tab. You can click the + button to add a new parameter and choose the type you want to create. In the example below we see each type: HSpeed and VSpeed are floats (0.0), Sprinting, Aiming and Moving are booleans (squares), Fire is a trigger (circle), and Turning is an int (0). You can also see how these can be used as the blendspace is being created from combinations of HSpeed and VSpeed with different animations at each 'point' in the space.
- Float A float input is a decimal. You can use this to control a blendspace. For example, having a float parameter called speed could control the transition between idle and walk.
- Int An integer input is a whole number. You can use this to control specific paths in a transition. For example, having an integer need to be equal to exactly 2 in order for a transition to occur.
- Bool A boolean is essentially a true or false value. You can use this to control conditional animations. For example, if you want a sprinting button you could have a boolean called 'sprint'. When sprint is true, you transition to another state. Bools are great for looping animations.
- Trigger Triggers are a specific type of boolean that fires as 'true' each time it's called. Triggers are great for one-shot style animations where you want to quickly set the animation to play and then have it continue.
The Mecanim system also features animation layering. You can use this to overlay and overwrite animation across the whole character or to control animation on specific parts of the body. A great example is having the base layer control body animation while a separate layer controls facial animation. All Animator Controllers start with a Base Layer. You can add more layers with the + button in the layer menu along the left side of the animator window.
Layers have several other options that can be used to configure them.
- Masks Layers have the ability to control the whole layer or specific parts of the body. If you want a layer to only affect the head, for example, you could assign an Avatar Mask to the mask of an animation layer. Avatar Masks can be created for humanoid or generic. Humanoid masks block out 'sections' while generic masks block out specific bones.
- Blending You can set your layer to additive (the animation will be added to the base animation) or override (the animation will replace the base animation). Override is generally used. Additive will require strong understanding of the base animation and how the layered animation will affect it.
- Sync (Unity Pro) You can also have multiple layers that do the same thing in slightly different ways. For example, if you have a 'normal' version of your character performing actions and a 'flying' version, you can use Sync to duplicate all of your logic. Syncing will copy all of the state machines of a layer but allow you to apply different animations. This is a great way to add depth without adding parameters and sub state machines.
- Weight You can set the weight of a layer via script (Animator.SetLayerWeight(String, Float)). The weight of a layer is essentially how much influence it has on the animation as a whole. 0 weight is 0 influence while 1 weight is complete influence.
Animator Scripting (Unity 4)
The animator component and controller work together but you also need to pass information in through scripts in most cases.
- this.GetComponent<Animator>() This will get the animator component on the game object the script is attached to.
- Animator.SetInteger(String, Int) Sets an integer parameter.
- Animator.SetFloat(String, Float) Sets a float parameter.
- Animator.SetBool(String, Bool) Sets a boolean parameter.
- Animator.SetTrigger(String) Sets a trigger parameter.
Animator Scripting (Unity 5)
Unity 5 brings a much more robust way of accessing your animations and allows you to script the states themselves using the StateMachineBehaviour class. This will access the animator its attached to for you and get it, the state info, and layerindex. Just hit "Add Behaviour" (n the Inspector) with any state selected to add a script to it! For example, you could have a state called "laughing" and add a script called "laughsounds". Adding that script to the laughing state will have it automatically play laughing sounds while in the laughing state!
You can use the following functions with these scripts to control when things occur in regards to states:
- OnStateEnter When the state begins (it is transitioned to or played by default)
- OnStateUpdate While the current state is this state, this is called on the Update loop.
- OnStateExit When the state stops (it is transitioned out of)
- OnStateIK Used for setting up IK on each state
Humanoid Rigs and Avatars
As briefly explained earlier, the avatar is essentially a definition of your rig. Unity has created a basic 'humanoid' definition inside of the engine so when you import your rig you can define it as also being humanoid. This means you're drawing a map between Unity's 'idea' of a humanoid rig and your rig so that Unity knows what bone you're talking about when you say 'left arm'.
Generic Rigs and Avatars
A generic rig could be anything from a tree to a dragon. Generic just means 'does not meet the definition for humanoid'. For generic rigs the avatar you're creating is making a new definition based off of your bones.
What's the Difference?
The key difference is that animations that are imported on a humanoid rig will use their mapping to Unity's humanoid to translate that animation to any other humanoid rig even if the rig is different. If you're working with fairly normal human type characters then humanoid is a great way to transfer animations between your characters easily.
For generic, you can only reuse animations on identical generic rigs. You could use dragon animations on another dragon that has the same rig, but not on a crocodile even though the bone structure is similar.
Defining An Avatar
By default Unity will define a new avatar for every .fbx import. If you're importing animations as their own .fbx files you don't need a new definition for each animation. The ideal setup is to import your bind-pose character .fbx with the mesh and create a new Avatar. Then, import the animations on that character as skeleton only and have them copy the avatar from the t-pose. This means you only have to keep one 'definition' of the rig, one copy of the mesh, and one import of the textures and materials.
Put it Together
When you combine all of this information, it's easy to set up animation from very basic walk and run blendspaces to complex multi-layer state machines. We've created a detailed example scene and tutorial that goes along with it if you're interested in learning more!