- Home /
Manipulate multiple animations speed
I have many animations in many layers. At a time, I want to pause an animation in a certain layer and trigger/resume another animation in another layer, for example.
If I change the Animator.speed, it will affect all the animations within.
How can I resolve this? Thanks for your help.
Answer by HoweToGaming · Mar 04, 2021 at 10:24 AM
I have multiple enemies on one layer on my level I want to start at different times and speeds so I have this piece of code
using UnityEngine;
public class AnimationControlerScript : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
Animator anim = GetComponent<Animator>();
AnimatorStateInfo state = anim.GetCurrentAnimatorStateInfo(0);//could replace 0 by any other animation layer index
anim.speed = Random.Range(0f, 2f);
anim.Play(state.fullPathHash, -1, Random.Range(0f, 1f));
}
}
And I'm sure when looking for this I saw you could also target particular animations
anim["animationName"].speed = 1f;
So that mixed with AnimatorStateInfo should get you there
He may of had a class and array to control speeds then that i didnt see sorry.
Not sure if this will help or not https://forum.unity.com/threads/change-speed-of-animation-for-a-particular-animator.428154/
Would something like this work
if (animator.GetCurrentAnimatorStateInfo(0).IsName("animation")) // if animation on layer 0
{
animator.speed = 0.5f;
}
else if (animator.GetCurrentAnimatorStateInfo(1).IsName("animation2")) // if animation 2 on layer 1
{
animator.speed = 0.75f;
}
else
{
animator.speed = 1f;
}
Answer by nmlinh2799 · Mar 04, 2021 at 02:37 PM
Thanks to @HoweToGaming I made a bit of research and find out there are already some answers, but none of them I see as clearly explained. I will make it simple.
Go to the Animator window, click on an animation state you want to adjust the speed, find Multiplier in the Inspector and check the Parameter checkbox, like this.
It will say you have to create a Float Parameter, so just create one called multiplier and set the default value 1.0, for example. The Inspector will be updated as below.
In the script, call animator.SetFloat("multiplier", speedScaleAsYouLike). Unity will calculate the actual animation speed by multiplying them together. Hope this helps.
Your answer
Follow this Question
Related Questions
2D Animation does not start 1 Answer
[C#] [2D] Animation mostly not working 1 Answer
Building Tilemap Animations in code require AnimationController 0 Answers
Original 3rd person constroller animator settings. 0 Answers
Animations out of sync? 0 Answers