Book Image

Unity 5.x Animation Cookbook

By : Maciej Szczesnik
Book Image

Unity 5.x Animation Cookbook

By: Maciej Szczesnik

Overview of this book

This recipe-based practical guide will show you how to unleash the power of animation in Unity 5.x and make your games visually impeccable. Our primary focus is on showing you tools and techniques to animate not only humanoid biped characters, but also other elements. This includes non-humanoid character animation, game world creation, UI element animation, and other key features such as opening doors, changing lights, transitioning to different scenes, using physics, setting up ragdolls, creating destructible objects and more. While discussing these topics, the book will focus on mecanim, the Unity 3D animation tool, and how you can use it to perform all these tasks efficiently and quickly. It contains a downloadable Unity project with interactive examples for all the recipes. By the end of this book, you will be confident and self-sufficient in animating your Unity 3D games efficiently.
Table of Contents (17 chapters)
Title Page
Credits
About the Author
About the Reviewer
www.PacktPub.com
Preface
Index

Importing skeletal animations


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.

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:

  1. Export a skinned model and its skeleton (called rig in Unity) to FBX format.

Note

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.

  1. Drag and drop the exported file into any subfolder in the Assets folder in your project or go to AssetsImport new asset and choose your FBX file.
  1. Select the imported file and navigate to the Inspector tab. Click on the Rig button:
  1. 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.
  2. Leave all the other inputs as defaults.
  1. Click on the Animations button. If an Unapplied import settings window appears, click on Apply.
  1. Make sure the Import Animation checkbox is checked.
  2. 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.
  1. 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.

Note

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.