0
\$\begingroup\$

I'm trying to play two animation clips sequentially using Unity 2D. I've been using Mecanim for this so I've imported the two clips in a newly created controller and connected the first one to the next one. I also want the last clip to be looping so I set that toggle in the settings of the clip.
Now for some reason the first clip is played just fine but the second one doesn't animate at runtime. It loops fine in the Animator window and the clip itself works in the preview window but not at runtime.

I've also tried setting it up using the Animation Component and use the PlayQueued method but this doesn't give any animation.

I'm kind of lost on what to try next

EDIT: This is to be used on a Canvas UI Image

\$\endgroup\$

2 Answers 2

1
\$\begingroup\$

You can just setup a transition between the two animations like so:

enter image description here

So in this above example, once the RedBlink animation is done playing once, it will automatically start playing RedAngry animation.

Make sure to only set the Loop Time on the animation that you want to loop.

enter image description here

\$\endgroup\$
18
  • \$\begingroup\$ Unity man :D (Y) \$\endgroup\$ Commented Feb 25, 2016 at 14:56
  • \$\begingroup\$ BTW I never tried this like this, if it really works then good answer bro \$\endgroup\$ Commented Feb 25, 2016 at 14:58
  • \$\begingroup\$ @HamzaHasan Really? Maybe you should reply with your method. It would be interesting to see how you do it. \$\endgroup\$
    – jgallant
    Commented Feb 25, 2016 at 14:59
  • \$\begingroup\$ @Jon I tried this and it gives me the same result. The first clip plays just fine and the second one goes into the loop but the animation on the screen seems to stop at the last frame of the first clip (+ see edit, I need this to work on a Canvas UI Image) \$\endgroup\$
    – Dries
    Commented Feb 25, 2016 at 15:01
  • \$\begingroup\$ @Dries Are you sure that second animation is not bugged? Can you play it manually? \$\endgroup\$
    – jgallant
    Commented Feb 25, 2016 at 15:02
0
\$\begingroup\$
using UnityEngine;

[RequireComponent(typeof(Animation))]
public class VAB_AnimationTester : MonoBehaviour
{
    public AnimationClip[] clips;
    public bool loop = true;

    private Animation _anim = null;


    void Start()
    {
        _anim = gameObject.GetComponent<Animation>();

        QueueClips(clips);
    }

    void Update()
    {
        if (!_anim.isPlaying && loop)
            QueueClips(clips);
    }

    void QueueClips(AnimationClip[] animclips)
    {
        for (int i = 0; i < animclips.Length; i++)
        {
            _anim.AddClip(animclips[i], animclips[i].name);
            _anim.PlayQueued(animclips[i].name, i == 0 ? QueueMode.PlayNow : QueueMode.CompleteOthers);
        }
    }
}

enter code here
\$\endgroup\$
3
  • \$\begingroup\$ This answer would be better if it included some text or comments to explain what this code does and how it solved the problem. \$\endgroup\$
    – DMGregory
    Commented Mar 31, 2022 at 19:19
  • \$\begingroup\$ Oh man !!! I'm ever so sorry that i posted a simple answer to animation sequencing in unity and it did not suit your needs. But all the code does is move through a sequence of animation clips by populating the animation queue. It will loop back if you set loop = true \$\endgroup\$
    – Mark Day
    Commented Apr 1, 2022 at 8:02
  • \$\begingroup\$ Great, edit that into the answer and we can delete these comments so the answer stands on its own. \$\endgroup\$
    – DMGregory
    Commented Apr 1, 2022 at 10:53

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .