- Home /
Queued animations
Is there a way to queue animations based on button presses?
Example: the a button plays anim1 while the b button plays anim2. So if I make this combination (a,a,b,a) then the animations while play accordingly one after the other in that exact sequence.
Answer by Llama_w_2Ls · Apr 19, 2021 at 06:12 PM
You could use a coroutine, paired with a check to see if anim1 has finished playing, before playing anim2. Here's a simple example:
public Animator Animator;
IEnumerator QueueAnimation(string thisClipName, string nextClipName)
{
Animator.Play(thisClipName);
// Wait till it's finished
while (Animator.GetCurrentAnimatorStateInfo(0).IsName(thisClipName))
{
yield return null;
}
Animator.Play(nextClipName);
}
When the coroutine is started, for example like: StartCoroutine("Anim1", "Anim2");
, the first animation clip is played, then the second, after the first has finished. You can convert this into a continuous method that loops through a list of strings as such:
public Animator Animator;
public List<string> QueuedClips;
IEnumerator PlayQueuedAnimations(int index)
{
// Exit once all queued clips have been played
if (QueuedClips.Count <= index)
yield break;
string thisClipName = QueuedClips[index];
Animator.Play(thisClipName);
// Wait till it's finished
while (Animator.GetCurrentAnimatorStateInfo(0).IsName(thisClipName))
{
yield return null;
}
StartCoroutine(PlayQueuedAnimations(index + 1));
}
Hope that's clear @Mrroundtree22
Pressing buttons A and B, should add the names of the assigned animation to the queue, and they will play once the coroutine reaches it.
Thank you so much! I didn't expect to get such a thorough response.
Your answer
Follow this Question
Related Questions
Can the animation editor create local rotational data? 3 Answers
Adding animation clips via script 2 Answers
How can I tell Unity to wait to do something until after and animation has finished playing? 1 Answer
isOpen queues next door to run close animation even if its closed 0 Answers
How can i play animations in a queue using the Animator? 0 Answers