Reader small image

You're reading from  Unity 5.x Animation Cookbook

Product typeBook
Published inMay 2016
Reading LevelExpert
PublisherPackt
ISBN-139781785883910
Edition1st Edition
Languages
Tools
Right arrow
Author (1)
Maciej Szczesnik
Maciej Szczesnik
author image
Maciej Szczesnik

Not interested. Too busy with current game project. Source: Linkedin.
Read more about Maciej Szczesnik

Right arrow

Adjusting the playback speed of animations


Unity allows you to slow down and speed the animation playback in the Animator Controller. You can do it in runtime with scripts to achieve interesting effects, for instance, slow motion.

Getting ready

Before we start, you should prepare and import a rig with at least one animation and create an Animator Controller with at least one animation state for it. You can also use the provided example Unity project and go to the Chapter 01 Working with animations\Recipe 08 Adjusting the playback speed of animationsdirectory. There is a scene called Example.unity there. In the scene Hierarchy, you can find and AdjustSpeed game object. It has an attached Animator Controller in which you can find two animation states: WaveSpeedNormal and WaveSpeedIncreased. There is also an AdjustSpeedByScript game object in the scene. You can increase the playback speed of its animations by pressing the Space button on your keyboard in runtime.

How to do it...

To change the animation playback speed, follow these steps:

  1. Open an Animator Controller.
  2. Select an animation state.
  3. Go to the animation state Inspector and find the Speed parameter below the Motion field.
  4. Enter a number in the Speed parameter to change the playback speed.

How it works...

The Speed parameter set for an animation state in the Animator Controller multiplies the speed playback of the animation state. Setting this parameter to zero will freeze the animation.

There's more...

You can also set the parameter using scripts. Following is an example script (it is used by the AdjustSpeedByScript game object in the provided Example.unity). You can assign it to your animated game object that has the Animator component and an Animator Controller attached:

using UnityEngine; 
using System.Collections; 
 
public class AdjustSpeedByScript : MonoBehaviour { 
//This is a variable, in which we store the reference to the 
Animator component 
private Animator anim; 
//We store the wanted animation speed in this variable, the 
default value is 2 (200%). 
 
public float newAnimationSpeed = 2f; 
void Start () { 
//At the start of the game we assign the Animator 
component to our anim variable 
anim = GetComponent<Animator>(); 
} 
void Update () { 
//We check if player pressed the Space button 
if (Input.GetKeyDown(KeyCode.Space)) { 
//And set the playback speed of the whole Animator 
Controller (it multiplies all states animation 
playback speed) 
 
anim.speed = newAnimationSpeed; 
} 
} 
} 

If you want to change the speed of just one animation state, then add a float parameter to your Animator Controller, use this parameter in the Multiplier field in the animation state Inspector, and change the parameter with scripts using the following function:

anim.SetFloat(string name, float value); 

Here name is the name of your parameter in the Animator Controller and value is the float value you want to set the parameter and playback speed to.

Previous PageNext Page
You have been reading a chapter from
Unity 5.x Animation Cookbook
Published in: May 2016Publisher: PacktISBN-13: 9781785883910
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime

Author (1)

author image
Maciej Szczesnik

Not interested. Too busy with current game project. Source: Linkedin.
Read more about Maciej Szczesnik