- Home /
How can I play a few animation clips one after another?
How can I play a few animation clips one after another? I have clipA, clipB and clipC. In my code, I want to play these clips randomly, but one after another. So it could start from clipB first, then after clipB is done playing, go on to play clipC, then clipA and so on.
I have tried PlayQueued. But it doesn't work as I expected it to be.
Here's my usage:
animation.PlayQueued("clipB", QueueMode.CompleteOthers);
animation.PlayQueued("clipA", QueueMode.CompleteOthers);
animation.PlayQueued("clipC", QueueMode.CompleteOthers);
animation.Play();
But this doesn't play the clips at all.
PlayQueued should work perfectly.
I used it myself to do the same.
You $$anonymous$$d showing the code? maybe we can see what's wrong.
This is quite old but for people who may stumble upon this in the future, be sure that the animation is not set to loop, otherwise it won't play queued animations.
Answer by fafase · Mar 30, 2013 at 06:38 PM
Here is a custom version:
IEnumerator QueueAnim(params AnimationClip[] anim){
int index = 0;
animation.clip = anim[index];
while(index<anim.Length){
animation.Play;
yield return new WaitForSeconds(anim[index].length);
index++;
animation.clip = anim[index];
}
}
Then you call it as such:
void Update(){
if(animQueue){
StartCoroutine(QueueAnim(animation["Anim1"],animation["Anim2"]));
animQueue = false;
}
}
I am doing this on top of my head with no Unity to try so there could be issues... You can pass as many animation clips as you wish since it uses params. But again, it could be all wrong...
Answer by dorpeleg · Mar 30, 2013 at 06:14 PM
Here is a function I made that works for me:
void RandomizeAnimation (GameObject crowd) {
int RndAnim = UnityEngine.Random.Range (0, Animations.Length);
int RndSpeed = UnityEngine.Random.Range (1, 100);
float RndFade = UnityEngine.Random.Range (0.1f, 1f);
crowd.animation.CrossFadeQueued("anim"+RndAnim, RndFade, QueueMode.CompleteOthers);
crowd.animation["anim"+RndAnim].speed = RndSpeed;
}
The gameobject have many animations, all animations are named anim1, anim2, anim3 etc.
I hope you can understand it, if you have any question just ask :)
Thanks! Is this function run in the Update or Start function? It's weird because when I use this, the animation doesn't run at all. I had it inside my Update function though. And those animations that I have are not made in Unity but animations that come with an fbx model. Would this matter?
Yes, it should be called from the Update() function.
$$anonymous$$ake sure you have the Animation Component on your gameobject and that all of your animation are listed in that component.
Your answer
Follow this Question
Related Questions
how can I play an animation from a different gameobject? 0 Answers
Issue with using additive animation clips imported from one .FBX 0 Answers
add panel between two animation clips in animator 0 Answers
Accessing a Mecanim state through scripting, then changing the associated clip--Can it be done? 2 Answers