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 animations: Wave 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:
- Select the animated asset and go to the
Animationstab. - Check the
Loop Timecheckbox and click on theApplybutton. The animation is looped. - If your animation's first and last frames don't match perfectly, you can force them to match with the
Loop Posecheckbox. 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:
- Select the animated asset and go to the
Animationstab. - Select your animation and make it loop (
Loop Timecheckbox). - Enter a value in the
Cycle Offsetfield, below theLoop Posecheckbox. - Click on the
Applybutton.
To mirror an animation, you need to go through the following steps:
- Select the animated
Humanoidasset and go to theAnimationstab. - Find the
Mirrorcheckbox on the bottom of the animation settings. - Check the
Mirrorcheckbox and click on theApplybutton. The animation is mirrored. - Mirroring animations works only for
Humanoidrigs.
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
Mirroroption 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 4, Character Movement. - Mirroring animations: This option works only with
Humanoidrigs. It is used to flip the animation left to right and can save up to 50 percent of steering animations when combined with theOffset Cycleoption.
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.