- Home /
How to change animation's speed in C#?
Hello everyone!
I am trying to add a sprint option for my top down 2D game. I want the speed to double, but the animations looks weird with it because its slower than it should be. How can i change the speed at which the animation plays through by means of a C# script?
Thank you!
Answer by Fariborzzn · Dec 18, 2019 at 10:38 PM
take a look at : https://answers.unity.com/questions/913840/can-i-change-animation-speed-with-mecanim.html
Answer by Michael_Berna · Dec 19, 2019 at 03:27 AM
AnimationState.speed
https://docs.unity3d.com/ScriptReference/AnimationState-speed.html
using UnityEngine;
using System.Collections;
public class ExampleScript : MonoBehaviour
{
public Animation anim;
void Start()
{
// Walk backwards
anim["Walk"].speed = -1.0f;
// Walk at double speed
anim["Walk"].speed = 2.0f;
}
}
Animator.speed
https://docs.unity3d.com/ScriptReference/Animator-speed.html
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Example : MonoBehaviour
{
Animator m_Animator;
//Value from the slider, and it converts to speed level
float m_MySliderValue;
void Start()
{
//Get the animator, attached to the GameObject you are intending to animate.
m_Animator = gameObject.GetComponent<Animator>();
}
void OnGUI()
{
//Create a Label in Game view for the Slider
GUI.Label(new Rect(0, 25, 40, 60), "Speed");
//Create a horizontal Slider to control the speed of the Animator. Drag the slider to 1 for normal speed.
m_MySliderValue = GUI.HorizontalSlider(new Rect(45, 25, 200, 60), m_MySliderValue, 0.0F, 1.0F);
//Make the speed of the Animator match the Slider value
m_Animator.speed = m_MySliderValue;
}
}
Your answer
Follow this Question
Related Questions
How to fix this animator is not playing an animatorcontroller? 1 Answer
Why when creating new animator controller for the character the character is not walking right ? 0 Answers
How can i check if animation has finished playing if the object have no animator attached ? 1 Answer
Animations out of sync? 0 Answers
Animator is not playing a Playable 3 Answers