Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Unity 5.x Animation Cookbook

You're reading from   Unity 5.x Animation Cookbook An advanced solution to all your Animation problems

Arrow left icon
Product type Paperback
Published in May 2016
Publisher Packt
ISBN-13 9781785883910
Length 328 pages
Edition 1st Edition
Languages
Tools
Arrow right icon
Author (1):
Arrow left icon
Maciej Szczesnik Maciej Szczesnik
Author Profile Icon Maciej Szczesnik
Maciej Szczesnik
Arrow right icon
View More author details
Toc

Table of Contents (11) Chapters Close

Preface 1. Working with Animations 2. Working with the Animation View FREE CHAPTER 3. 2D and User Interface Animation 4. Character Movement 5. Character Actions and Expressions 6. Handling Combat 7. Special Effects 8. Animating Cutscenes 9. Physics and Animations 10. Miscellaneous

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.

lock icon The rest of the chapter is locked
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.
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 $19.99/month. Cancel anytime
Banner background image