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.
Importing skeletal animations
Getting ready
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.
How to do it...
To import a skeletal animation, you need to follow these steps:
- Export a skinned model and its skeleton (called rig in Unity) to FBX format.
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.
- Drag and drop the exported file into any subfolder in the Assets folder in your project or go to Assets | Import new asset and choose your FBX file.
- Select the imported file and navigate to the Inspector tab. Click on the Rig button:
- Here you can specify the type of the rig to use. For bipedal characters, use the 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.
- Leave all the other inputs as defaults.
- Click on the Animations button. If an Unapplied import settings window appears, click on Apply.
- Make sure the Import Animation checkbox is checked.
- Click on the Apply button in the lower right corner of the Inspector (just above the Animation Preview). You may need to scroll the Inspector down.
- You should be able to see all imported animation clips as child assets of your imported FBX file.
How it works...
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.
There's more...
- Imported animation clips are also listed in the Clips section of the Animations tab.
- You can add new clips by clicking on the plus button below the Clips section. You can also remove clips by clicking on the minus button.
- You can rename each clip by using the input field above the Source Take drop-down menu.
- You can choose the source take (the animation stored in your FBX file) for each animation clip with the Source Take drop-down menu.
- You can also trim the animation clip by editing the 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.