The early Unity versions’ Legacy Animation System is used in Unity for a wide range of things, such as animating the color of a light or other simple animations on 3D objects in a scene, as well as animating skinned characters for certain kinds of games.
In this tutorial, we will look at the basic settings for the Legacy Animation System. Then, we will step into the new animation system, gaining an understanding of the ThirdPersonCharacter prefab, and looking at the difference between the in-place and Root Motion animation methods available within Animator.
If you want to dive deep into developing cutting-edge modern day Unity 2D games then this piece is for you. We will deal with character animations using Unity today.
This article is an excerpt from the book Unity 2017 Game Development Essentials written by Tommaso Lintrami.
Importing character models and animations
To import a model rig or an animation, just drag the model file to the Assets folder of your project. When you select the file in the Project view, you can edit the Import Settings in the Inspector panel:
Importing animations using multiple model files
The common way to import animations in Unity is to follow a naming convention scheme that is recognized automatically. You basically create, or ask the artist to create, separate model files and name them with the [email protected] convention.
For example, for a model called Warrior_legacy, you could import separate idle, walk, jump, and attack animations using files named [email protected], [email protected], [email protected]_run_inPlace.fbx, and [email protected]_inPlace.fbx. Only the animation data from these files will be used, even if the original files are exported with mesh data from the animation software package:
In the editor’s Project view, the .fbx suffix is not shown in the preview, but can still be seen in the bottom line of the view. Unity automatically imports all the files, collects all the animation clips from them, and associates them with the file without the @ symbol. In the example above, the Warrior_legacy.fbx file will be set up to reference offensive_idle, jumping, running_inPlace, and sword_and_shield_walk_inPlace.
To export the base rig, simply export a model file from your favorite digital content creation package with no animations ticked in the FBX exporter (for example, Warrior_legacy.fbx) and the four animation clips as [email protected] by exporting the desired keyframes for each one of them (enabling animation in the graphic package’s FBX export dialog).
When imported in Unity, we will select the main rig file ( Warrior_legacy.fbx) and set its Rig type to Legacy:
Setting up the animation
We need to instruct Unity on how we want to play these animation clips, for instance, we certainly want the walk, idle, and running animation clips to play in a loop, while the jump and the attack animation clips should play in a single shot.
Choose the Idle animation clip in the Project view folder where the legacy animation resides and then switch to the Animations tab in the Inspector:
Set the Wrap Mode to PingPong in both the top and bottom parts of the panel, as shown in the preceding image.
In many cases, you might also want to create an additional in-between loop frame, checking the Add Loop Frame option. This is needed to avoid an ugly animation loop being performed because the first and last frame of the animation are too different from each other. Click on the Apply button at the bottom of the panel to apply the changes.
This will be required if the first and last frames of the animation are much different and require an additional frame in-between to interpolate between the two in order to obtain a good loop for this Animation Clip. Now, drag the Warrior_legacy.fbx main file into the scene. You should see a new GameObject with an Animations component attached, with all the reference clips already set up, and with the first specified to play at start when the Play On Awake checkbox is selected in the component (default).
Building the Player with Animator
The Animator component was introduced in Unity 4 to replace the older Legacy Animation System. If you are completely new to Unity, you should start directly with the new animation system and consider the old one as still being good for many things, not only related to character animation. Animator introduced many cool things that were only partially available (and only through coding) with the old animation system.
What is an avatar in Unity?
When an .fbx 3D model file with a skeleton made of joints/bones is imported in Unity, if you expand the file in the Project view, you will see, among the various parts of it, an avatar’s icon. The following screenshot represents the Warrior_Final.fbx rigged model automatically created by the Warrior_FinalAvatar component:
When importing a rigged model instead (an FBX model with a skeleton or bones and, optionally, animations), Unity will configure it automatically for a Generic avatar. A Generic avatar is meant for any kind of non-human character rig, such as animals, non-biped monsters, plants, and so on. Typically, for your biped/humanoid characters, you want to switch the default import flag for the Rig Animation Type to Humanoid:
As the default import setting for the Rig is Generic, we will switch to Humanoid for all the .fbx files in the Warrior_Mecanim_InPlace folder with the only exclusion being the non-rigged Warrior_final_non-rigged.fbx sample model mentioned earlier.
Configuring an avatar
Now, hit the Configure button and the actual scene and the Inspector will be temporarily replaced with the avatar, (as in the following screenshot), until the Done button is clicked and the editor returns to the previously loaded scene.
Hands mapping will be used only when your characters need fine finger movements hence will not be covered.
The best scenario for this is a game where characters perform a lot of specific actions with many different tools (guns, hammers, knives, hacking, or maybe just making gestures while talking during a cinematic cut scene). Another example would be an avatar for virtual reality, where the Leap Motion, Data Gloves, or similar devices are used to track the hands of users with the 3 phalanges of their 10 fingers.
If the rig you are importing is not Mecanim-ready, this is the place to map your bones to the appropriate spots on the green diagram in the Inspector, which is subdivided into body, head, left hand, and right hand.
To configure an Avatar from a model that was not rigged following Mecanim’s skeleton rules, we have the following two options:
- Using the auto-mapping feature available, which will try to automatically map the bones for you
- Manually map the bones of your model to the corresponding spots on the diagram
The avatar configuration Inspector panel shows the skeleton’s main bones mapped to the avatar: