Tharres
Tharres

Reputation: 113

Play animator clips one after another

I have a list of clips from one animator. How do I play each of these clips one after another such that when first ends, second begins and so on..

public Animator anim;
    // Start is called before the first frame update
    void Start()
    {
        StartCoroutine(PlayAllAnimations());
    }

    // Update is called once per frame
    void Update()
    {
        
    }


    IEnumerator PlayAllAnimations()
    {
        anim.Play("Anim_1", 0, 0.25f);
        yield return new WaitForSeconds(2);
        anim.Play("Anim_2", 0, 0.25f);
        yield return new WaitForSeconds(2);
        anim.Play("Anim_3", 0, 0.25f);
    }

Upvotes: 1

Views: 2005

Answers (1)

Leoverload
Leoverload

Reputation: 1240

You should use the Animator to do these things because it's really useful and easy to use. For example:A simple animator with animation system In this case, I have put the 3 animations in the animator and select one as the idle state, and then added transitions in the order I wanted without a condition. Leaving an transition without a condition means that the animations will directly go to the other one based on Has exit time. You can also uncheck the loop box in the animation so it will play just one time and go to the other animation immediately. In this way you can create simple loops between animations or anything you prefer with conditions and parameters! I suggest also to set the transition duration to zero if you want an immediate change between animations, but that is up to your tests!

If you prefer to work with script another way to do this is by working with Animation Event and with it, you can add events through the script without any problems.

For a more complex series of animations, you can use the Timeline, a Unity system that can make you work with many animations on a timeline in a more cinematic way.

Upvotes: 3

Related Questions