How do you run an animation only once?

Hi all,

I’ve never really animated in Unity before and was wondering how you would make an animation run only once? As far as I can see, there’s no button that automatically prevents it from looping. Currently it just runs infinitely until it’s told not to. (If I hold right click it will loop until I let go, i would prefer it only run once and then stop). I have an animator inside of the object, and the animation works fine so this is (right now) the only problem I have. If there’s any scripts involved, I’d prefer C# but I can’t complain about JavaScript so long as it works.

Thanks,

Pierce

You can setup the animation to play when a Trigger parameter is activated.

Here is how you can setup the animator:

You can select/deselect Exit time on part 2 as you wish. It will determine if the animation is fully executed before transitioning to the next animation (empty state in this case).

Then make a script and attach it to the same object as the animator is, and set the trigger using:

yourAnimator.SetTrigger("ExampleTrigger");

If you want to start the animation with a mouseclick use:

if (Input.GetMouseButtonDown(1))
{
    yourAnimator.SetTrigger("ExampleTrigger");
}

This will set the trigger, which will transition to the animation and disable the trigger automatically, so that you will have to click again to activate the trigger again and play the animation.

Set up an Idle animation state in mecanim. Then, set up a state with transitions for whatever other animation you want to play without looping. You can check off “Exit Time” for the latter animation, so it will revert to idle after it’s done. You will need scripts for the transitions, but everything else is done in mecanim.