How to restart unity animator?

JongHyun Kim picture JongHyun Kim · May 20, 2017 · Viewed 12.5k times · Source

I used an animator to count down (three seconds using the finger model). I succeeded in making a countdown animator. I want to play the animator whenever I want because this project requires countdowns several times for calibration.

But I do not know how. Now I can not play the animator when I want, nor do I know how to play it again. Currently, the animator is created in the animator window, not as a script.

enter image description here

Answer

Kokolo picture Kokolo · May 20, 2017
animator.Play("animation name", layer, normalizedTime);

This is the overload. More precisely do this:

animator.Play("your animation name", -1, 0f);

If you want, you can use co-routine as well. It's my preferred way:

private IEnumerator Counter(int secondsToCount)
{
    while (secondsToCount >= 0)
    {
        //'counter' is the counter that appears to the player
        counter.text = secondsToCount.ToString();
        secondsToCount--;
        yield return new WaitForSeconds(1f);
    }
}

This way you have control how many seconds you want to count down.

EDIT:

If you are new and know nothing of co-routines. You can start them like this:

StartCoroutine(Counter(5));

This will count from 5 to 0.