- Home /
Stop a animation loop after a certain amount of plays?
How would i make an animation Stop after it has looped 5-6 times? I am pritty new to coding and i want an animation to play 5-6 times before it just stops.
Answer by jerobinson · May 27, 2013 at 12:04 PM
There's only one way that I can think of to do this, and there are probably better / more correct ways, but this should work for the time being...
using UnityEngine;
using System.Collections;
public class AnimationScript : MonoBehaviour {
public int repeatNumber = 6;
void Start() {
animation["Your Animation"].wrapMode = WrapMode.Loop;
StartCoroutine(LoopAnimation());
}
IEnumerator LoopAnimation() {
animation.Play("Your Animation");
yield return new WaitForSeconds(animation["Your Animation"].length * repeatNumber);
}
}
So this script basically just creates an IEnumerator function, inside of which it starts the looping animation, and then waits for the number of seconds that it will take to play however many loops that you want.
It is untested and may require some tweaking.
Good Luck
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Problem with animation. 1 Answer
When 2 buttons pressed animation stops? 1 Answer
Making an animation stop once it finishes (trouble with booleans) 1 Answer
How do i loop animation 1 Answer