Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change the speed of animation at runtime in unity c#

I want to change the speed of animation to my desired speed during runtime of a program. I have an c# script and animator controller attached to the game object. The default speed of animation in unity is 1. I set the default speed value of animation to 0.3f. And during runtime of program, I want the speed of animation to 1.

using UnityEngine;
using System.Collections;
public class wowBoard : MonoBehaviour {
    [SerializeField]
    Animator anim;
    bool changeSpeed;
   void Start()
   {
      anim=GetComponent<Animator>();
     playAnim();
     changeSpeed=false;
    }

   public void playAnim()
   {
     anim.SetBool("show",true);
   }

   void Update()
  {
     if(changeSpeed)
        playChangeSpeedAnim();

   }

 public void playChangeSpeedAnim()
 {
    anim.speed=1;
    anim.SetBool("show",true);
 }
}

The speed of animation did not get change to 1 even boolean value of changeSpeed is true.

like image 448
ero Avatar asked Sep 16 '16 06:09

ero


People also ask

How do I change the animation speed in runtime Unity?

To manipulate the animation speed you can use the Speed multiplier parameter field in the animation properties. If you select the animation in the animator, you will see in the inspector this: If you click on the "Parameter" checkbox, the Multiplier option will be enabled.

How do I slow down an animation in Unity?

First, open the state machine associated with this animator by double-clicking on the controller field of your object. Then select the animation you would want to slow down in the state machine. Then in Inspector panel there is a speed field. You can just decrease its value to slow the animation down.


1 Answers

To manipulate the animation speed you can use the Speed multiplier parameter field in the animation properties.

If you select the animation in the animator, you will see in the inspector this:

If you click on the "Parameter" checkbox, the Multiplier option will be enabled. In the Multiplier field, you can select a float type parameter, and the animation speed will be set the parameter's value.

So, you can just create a new float parameter named speed, and change the speed of the animation to X through script by just executing anim.SetFloat("speed", X);

like image 57
Leo Avatar answered Sep 23 '22 13:09

Leo