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

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.

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:

I suggest you use Coroutines (Unity - Manual: Coroutines) 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.