9 min read

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 character models and animations
Please refer to the updated Unity online manual for a full description of the available import options: https://docs.unity3d.com/Manual/FBXImporter-Model.html.

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], Warrior_legacy@standard_run_inPlace.fbx, and Warrior_legacy@walking_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:

A list of animation clip files for an animated character

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:

Animations tab

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).

You can look at the final result for this part in the Chapter5_legacy Unity scene in the book’s code project folder.

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.

In the code folder, under Chapter 5-6-7/Models/Characters, you will find three folders for the warrior model rig. One is meant for the old Legacy Animation component, and the other two are for use with the Animator.
The new system is made by a new animation component, the Animator, a powerful state-machine that controls the whole animation process and the Avatar configuration system. The Animator component will be mapped to a corresponding avatar and to an Animator Controller asset file, which can be created, like other files, from the Project view and edited in the Animator window.

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:

avatar in Unity

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:

Biped character This term comes from the Latin word bi (two) and ped (foot); this 3D animation-specific term indicates an anthropomorphic/humanoid character standing and walking on two legs. This name was introduced into 3D animation by 3D Studio Max, where Biped was the term for Character Studio to manage a rigged human character and its animations.

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.

Because the model included in the book codes is already a Mecanim-ready rig, you can just click on the Done button.
Configuring an avatar
On the left, the Scene view switched temporary for showing Avatar configuration results and the Inspector on the right showing configuration options.
Most of the time, and in this case, you will not set the mapping for hands, fingers, and eyes separately, so the first of the four tabs (Body, Head, Left Arm, and Right Arm) will be enough for our purpose. The head is usually mapped for independent eyeball movement and/or jaw movement to allow a basic character speech movement whenever your game needs any of these features. The head part of the avatar configuration Inspector panel is shown as follows:
avatar configuration Inspector panel
A quick note on lip sync. Lip sync is an advanced technique, where a 3D character’s face will change and animate its mouth and eyes when a certain audio file is playing. Unity doesn’t support lip sync out of the box, but it can be done in many ways with external libraries and an appropriate model rig. Since Unity 4.5 onward, animation Blend Shapes are supported, allowing facial muscles’ gestures to be embedded in the .fbx model and used in real time by the application.
This technique is more modern than standard lip sync for game character’s speeches; in both cases, a library or a discreet amount of coding would be needed to make the character speak correctly when the corresponding audio file is played.

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:

avatar configuration Inspector
The Automap feature, accessible from the drop-down menu at the bottom-left part of the avatar Inspector, can automatically assign the bones of your models to the correct ones for a mecanim rig. This is mainly performed by reading bone names and analyzing the structure of the skeleton, and it is not 100% rig proof. So, you might need some tweaking (manual mapping) of your custom character models.
As you can see, there are also Load and Save options to store this mapping. This is useful if you have a whole bunch of rigged character models all done with the same skeleton naming convention.
The Clear option will clear up all the current bone mapping. The Pose drop-down menu is needed only if you want to enforce the T-pose, or sample the actual pose, and is rarely needed, but can help fix eventual modeler/3D artist mistakes or to make variations of an avatar.
We discussed about creating Unity Character animations and how it will help you build interactive games with unity. Check out the book Unity 2017 Game Development Essentials for hands on game development in Unity 2017.

Read Next

Unite Berlin 2018 Keynote: Unity partners with Google, launches Ml-Agents ToolKit 0.4, Project MARS

AI for Unity game developers: How to emulate real-world senses in your NPC agent

Working with Unity Variables to script powerful Unity 2017 games

LEAVE A REPLY

Please enter your comment!
Please enter your name here