In this article by Jamie Dean author of the book Unity Character Animation with Mecanim: RAW, we will demonstrate the process of importing and animating a rigged character in Unity. In this article, we will cover:
- Starting a blank Unity project and importing the necessary packages
- Importing a rigged character model in the FBX format and adjusting import settings
Typically, an enemy character such as this will have a series of different animation sequences, which will be imported separately or together from a 3D package. In this case, our animation sequences are included in separate files.
We will begin, by creating the Unity project.
(For more resources related to this topic, see here.)
Setting up the project
Before we start exploring the animation workflow with Mecanim’s tools, we need to set up the Unity project:
- Create a new project within Unity by navigating to File | New Project….
- When prompted, choose an appropriate name and location for the project.
- In the Unity – Project Wizard dialog that appears, check the relevant boxes for the Character Controller.unityPackage and Scripts.unityPackage packages.
- Click on the Create button. It may take a few minutes for Unity to initialize.
- When the Unity interface appears, import the PACKT_cawm package by navigating to Assets | Import Package | Custom Package….
- The Import package… window will appear.
- Navigate to the location where you unzipped the project files, select the unity package, and click on Open.
The assets package will take a little time to decompress.
- When the Importing Package checklist appears, click on the Import button in the bottom-right of the window.
Once the assets have finished importing, you will start with a default blank scene.
Importing our enemy
Now, it is time to import our character model:
- Minimize Unity.
- Navigate to the location where you unzipped the project files.
- Double-click on the Models folder to view its contents.
- Double-click on the zombie_m subfolder to view its contents.
The folder contains an FBX file containing the rigged male zombie model and a separate subfolder containing the associated textures.
- Open Unity and resize the window so that both Unity and the zombie_m folder contents are visible.
- In Unity, click on the Assets folder in the Project panel.
- Drag the zombie_m FBX asset into the Assets panel to import it.
Because the FBX file contains a normal map, a window will pop up asking if you want to set this file’s import settings to read it correctly.
- Click on the Fix Now button.
FBX files can contain embedded bitmap textures, which can be imported with the model. This will create subfolders containing the materials and textures within the folder where the model has been imported. Leaving the materials and textures as subfolders of the model will make them difficult to find within the project.
The zombie model and two folders should now be visible in the FBX_Imports folder in the Assets panel.
In the next step, we will move the imported material and texture assets into the appropriate folders in the Unity project.
Organizing the material and textures
The material and textures associated with the zombie_m model are currently located within the FBX_Imports folder. We will move these into different folders to organize them within the hierarchy of our project:
- Double-click on the Materials folder and drag the material asset contained within it into the PACKT_Materials folder in the Project panel.
- Return to the FBX_Imports folder by clicking on its title at the top of the Assets panel interface.
- Double-click on the textures folder. This will be named to be consistent with the model.
- Drag the two bitmap textures into the PACKT_Textures folder in the Project panel.
- Return to the FBX_Imports folder and delete the two empty subfolders.
The moved material and textures will still be linked to the model. We will make sure of this by instancing it in the current empty scene.
- Drag the zombie_m asset into the Hierarchy panel.
It may not be immediately visible within the Scene view due to the default import scale settings.
We will take care of this in the next step.
Adjusting the import scale
Unity’s import settings can be adjusted to account for the different tools commonly used to create 2D and 3D assets. Import settings are adjusted in the Inspector panel, which will appear on the right of the unity interface by default:
- Click on the zombie_m game object within the Hierarchy panel.
This will bring up the file’s import settings in the Inspector panel.
- Click on the Model tab.
- In the Scale Factor field, highlight the current number and type 1.
The character model has been modeled to scale in meters to make it compatible with Unity’s units. All 3D software applications have their own native scale. Unity does a pretty good job at accommodating all of them, but it often helps to know which software was used to create them.
- Scroll down until the Materials settings are visible.
- Uncheck the Import Materials checkbox.
Now that we have got our textures and materials organized within the project, we want to make sure they are not continuously imported into the same folder as the model.
- Leave the remaining Model Import settings at their default values.
We will be discussing these later on in the article, when we demonstrate the animation import.
- Click on the Apply button. You may need to scroll down within the Inspector panel to see this:
The zombie_m character should now be visible in the Scene view:
This character model is a medium resolution model—4410 triangles—and has a single 1024 x 1024 albedo texture and separate 1024 x 1024 specular and normal maps.
The character has been rigged with a basic skeleton. The rigging process is essential if the model is to be animated.
We need to save our progress, before we get any further:
- Save the scene by navigating to File | Save Scene as….
- Choose an appropriate filename for the scene.
- Click on the Apply button.
Despite the fact that we have only added a single game object to the default scene, there are more steps that we will need to take to set up the character and it will be convenient for us to save the current set up in case anything goes wrong.
In the character animation, there are looping and single-shot animation sequences. Some animation sequences such as walk, run, idle are usually seamless loops designed to play back-to-back without the player being aware of where they start and end.
Other sequences, typically, shooting, hitting, being injured or dying are often single-shot animations, which do not need to loop. We will start with this kind, and discuss looping animation sequences later in the article.
In order to use Mecanim’s animation tools, we need to set up the character’s Avatar so that the character’s hierarchy of bones is recognized and can be used correctly within Unity.
Adjusting the rig import settings and creating the Avatar
Now that we have imported the model, we will need to adjust the import settings so that the character functions correctly within our scene:
- Select zombie_m in the Assets panel.
The asset’s import settings should become visible within the Inspector panel.
This settings rollout contains three tabs: Model, Rig, and Animations.
Since we have already adjusted the Scale Factor within the Model Import settings, we will move on to the Rig import settings where we can define what kind of skeleton our character has.
Choosing the appropriate rig import settings
Mecanim has three options for importing rigged models: Legacy, Generic, and Humanoid. It also has a none option that should be applied to models that are not intended to be animated.
Legacy format was previously the only option for importing skeletal animation in Unity. It is not possible to retarget animation sequences between models using Legacy, and setting up functioning state machines requires quite a bit of scripting. It is still a useful tool for importing models with fewer animation sequences and for simple mechanical animations. Legacy format animations are not compatible with Mecanim.
Generic is one of the new animation formats that are compatible with Mecanim’s animator controllers. It does not have the full functionality of Mecanim’s character animation tools. Animations sequences imported with the generic format cannot be retargeted and are best used for quadrupeds, mechanical devices, pretty much anything except a character with two arms and two legs.
The Humanoid animation type allows the full use of Mecanim’s powerful toolset. It requires a minimum of 15 bones, and assumes that your rig is roughly human shaped with a pair of arms and legs. It can accommodate many more intermediary joints and some basic facial animation.
One of the greatest benefits of using the Humanoid type is that it allows animation sequences to be retargeted or adapted to work with different rigs. For instance, you may have a detailed player character model with a full skeletal rig (including fingers and toes joints), maybe you want to reuse this character’s idle sequence with a background character that is much less detailed, and has a simpler arrangement of bones.
Mecanim makes it possible reuse purpose built motion sequences and even create useable sequences from motion capture data.
Now that we have introduced these three rig types, we need to choose the appropriate setting for our imported zombie character, which in this case is Humanoid:
- In the Inspector panel, click on the Rig tab.
- Set the Animation Type field to Humanoid to suit our character skeleton type.
- Leave Avatar Definition set to Create From This Model.
- Optimize Game Objects can be left checked.
- Click on the Apply button to save the settings and transfer all of the changes that you have made to the instance in the scene.
The Humanoid animation type is the only one that supports retargeting. So if you are importing animations that are not unique and will be used for multiple characters, it is a good idea to use this setting.
In this article, we covered the major steps involved in animating a premade character using the Mecanim system in Unity. We started with FBX import settings for the model and the rig. We covered the creation of the Avatar by defining the bones in the Avatar Definition settings.
Resources for Article:
Further resources on this subject: