- Home /
play animation sequence
Hello,
I want to play 2 (or more) consecutive animations. For example run then roll. How can i do it using C# script? and if i want to animate more than one player in a football game with different animations should i be using threads?
Give me a helpful link for consecutive animations and threads in unity if possible. thanks in advance
Answer by azrijamil · Sep 20, 2014 at 10:42 AM
You can use Observable class from Reactive Extension library. For Unity use UniRx from asset store, below is some example.
public class UIAnimationClip
{
public string clipName;
public int delayAfterPlay;
}
public IObservable<bool> PlayAsObservable(List<UIAnimationClip> animClips)
{
return Observable.FromCoroutine<bool>(observer => this._PlayAsObservable(animClips, observer));
}
private IEnumerator _PlayAsObservable(List<UIAnimationClip> animClips, IObserver<bool> observer)
{
foreach (UIAnimationClip animClip in animClips)
{
animation.Play(animClip.clipName);
while (animation.isPlaying)
{
yield return null;
}
Debug.Log("Finished playing : " + animClip.clipName);
if (animClip.delayAfterPlay > 0) yield return new WaitForSeconds(animClip.delayAfterPlay);
observer.OnNext(true);
}
observer.OnNext(false);
observer.OnCompleted();
}
To use it, just create a sequence of observable collection, this example is not optimize and it just for the example purposes.
void Start()
{
this.animationClips.Add(new UIAnimationClip{
clipName = "fade-in",
delayAfterPlay = 2
});
this.animationClips.Add(new UIAnimationClip{
clipName = "fade-out",
delayAfterPlay = 0
});
this.PlayAsObservable(this.animationClips).Where(p => p == false).Subscribe(stop =>
{
Debug.Log("Complete");
});
}
It will push true value after each play, and false if no more animation clip to play.
Answer by Saad_Khawaja · Jul 17, 2014 at 08:29 AM
You can use Animator Controller (Mecanim) on a GameObject for Animation. Mecanim uses state machines to create transitions between different animations.
For e.g. If you wanted to play Roll after Run, you could create two states - (the first being run and the second roll and attach the respective animations)
I would advise you to go through the Unity Mecanim tutorials here: http://unity3d.com/learn/tutorials/modules/beginner/animation
What if i have like almost 25 animations and i have to switch between 2 of them? for example i have those animations: run-roll-jump-kneel...! So at first i want to play run and roll and after like 2 seconds i want to play run and jump and then after 2 other i want to run roll then kneel. How can i do it with c# script using the animator controler?
Answer by Rigo.cl · Jul 17, 2014 at 11:25 PM
I suggest you use Coroutines (http://docs.unity3d.com/Manual/Coroutines.html) combined with animation["anim"].length, Coroutines are basic and very useful threads, something like this:
In your Update method:
if (!isPlaying) // nothing playing? let's play something...
{
string nameAnim;
// now assign 'animId' some random int between 1 and 25.
switch (animId)
{
case 1:
nameAnim = "anim1"; break;
case 2:
nameAnim = "anim2"; break;
case 3:
nameAnim = "anim3"; break;
case 25:
nameAnim = "anim25"; break;
}
// now an animation is running
isPlaying = true;
// now wait for the anim to finish plus 2 seconds more.
StartCoroutine(LetsWait(animation[nameAnim].length + 2));
}
Your Coroutine:
IEnumerator LetsWait(float secsToWait)
{
yield return new WaitForSeconds(secsToWait); // we wait here (not exactly actually)...
// done waiting... now update the playing flag to go for the next animation.
isPlaying = false;
}
If you don't want to wait always 2 seconds more, then create a float and inside every 'case' assign the additional seconds to wait.
Pretty much that's it.
regards.
Coroutines are not threads, even if they share some basic properties. They're more akin to the old cooperative multitasking model (taking turns running in sequence, passing control to the next by Yielding) than true threading (multiple processors and/or time splicing processor resources).
Your answer
![](https://koobas.hobune.stream/wayback/20220613154311im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Third Person Help 4 Answers
Reloading Help 2 Answers
Animation Scripting Help 2 Answers
Stop animation after played 30 times? 0 Answers
Animation flashlight when run 4 Answers