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

Looping, mirroring and offsetting the animations


Unity allows editing the animations to some extent after they're imported. It can save a lot of work and greatly speed up your workflow.

Getting ready

Before we start, you should prepare and import a Humanoid rig with at least one animation. You can also use the provided example Unity project and go to the Chapter 01 Working with animations\Recipe 07 Looping mirroring and offsetting animations directory. There is a scene called Example.file there. In the scene Hierarchy, you can find a Mirror game object. It has an attached Animator Controller in which you can find two animationsWave and WaveMirror. In the Rigs directory, you will find the Mirror.fbx asset. If you select it and go to the Inspector, and to the Animations tab, you can find normal and mirrored animation examples, as well as looped animation examples (Idle and IdleMirror).

How to do it...

To set an animation to loop, you need to go through the following steps:

  1. Select the animated asset and go to the Animations tab.
  2. Check the Loop Time checkbox and click on the Apply button. The animation is looped.
  3. If your animation's first and last frames don't match perfectly, you can force them to match with the Loop Pose checkbox. It is not recommended to use this option for animations that have matching first and last frames.

To offset an animation, you need to go through the following steps:

  1. Select the animated asset and go to the Animations tab.
  2. Select your animation and make it loop (Loop Time checkbox).
  3. Enter a value in the Cycle Offset field, below the Loop Pose checkbox.
  4. Click on the Apply button.

To mirror an animation, you need to go through the following steps:

  1. Select the animated Humanoid asset and go to the Animations tab.
  2. Find the Mirror checkbox on the bottom of the animation settings.
  3. Check the Mirror checkbox and click on the Apply button. The animation is mirrored.
  4. Mirroring animations works only for Humanoid rigs.

How it works...

  • Looping animations: This is a common technique used for all cyclic movements (walk and run cycles, idle animations, and so on). If you don't set an animation to loop, it will play once and freeze on the last frame.
  • Offsetting animations: Sometimes it is convenient to offset the cycle of a looped animation. It is often used with the Mirror option for steering animations (clips used to turn the character while moving). We will be showing that in the Using root motion to steer a character recipe in Chapter 4Character Movement.
  • Mirroring animations: This option works only with Humanoid rigs. It is used to flip the animation left to right and can save up to 50 percent of steering animations when combined with the Offset Cycle option.

There's more...

You can also mirror and offset animation states in the Animator Controller. If you select an animation state and go to the Inspector tab, you can find the Mirror and Cycle Offset options. There is also an option to use Animator Controller parameters to switch the Mirror option on and off and set the Cycle Offset. You need to have a Boolean parameter defined for the Mirror option and a float parameter for the Cycle Offset. Those settings will be automatically synchronized with the parameters. Whenever you change a parameter value, the setting will also be changed.