- Home /
Play animation 5 sec, stop for 5 seconds, repeat
ok how can I do this. I can play my anim no problems and stop it after 5 seconds but how to start it again?
I'm just confused with the logic of timers, invoking, coroutines, looping etc in C# and thats why I'm asking for help..
Answer by Cherno · Apr 13, 2015 at 10:27 PM
Something like this
public Animation animation;
void Start() {
animation = GetComponent<Animation>();
StartCoroutine(MyCoroutine());
}
void RepeatMyCoroutine() {
StartCoroutine(MyCoroutine());
}
private IEnumerator MyCoroutine() {
animation.Play("ClipName");
yield return new WaitForSeconds(5f);
animation.Stop("ClipName");
RepeatMyCoroutine();
yield return null;
}
or
public Animation animation;
public bool keepPlaying;
public int failSafe = 20;
void Start() {
animation = GetComponent<Animation>();
keepPlaying = true;
StartCoroutine(MyCoroutine());
}
private IEnumerator MyCoroutine() {
int cycles = 0;
while(keepPlaying == true) {
animation.Play("ClipName");
yield return new WaitForSeconds(5f);
animation.Stop("ClipName");
cycles ++;
if(cycles >= failSafe) {
yield break;
}
}
yield return null;
}
thanks!! that's nearly it, except it does not pause and it flips back to the start of the animation every time ins$$anonymous$$d of playing from the same frame.... to play previously I've used:
public string WhatAnimation = "Put Animation name here"; //user enters name of animation
public float WhatSpeed = 0.0f;
void Start () {
float Animlength = animation.clip.length; //gets the clip length
Animlength = (Animlength - 10); //takes 10 sec away
float $$anonymous$$yFrame = Random.Range(10f, Animlength); //random chooses a time
animation.Play(); //plays the animation clip on character
animation[ WhatAnimation ].normalizedTime = $$anonymous$$yFrame; //starts anim at that time
animation[ WhatAnimation ].speed = WhatSpeed;
then I've been setting what speed to 1.0 or 0.0 to start and stop
Your answer
Follow this Question
Related Questions
WaitForSeconds bug,Coroutine Bug 1 Answer
How to make WaitForSeconds work as a repeating/looping timer? 1 Answer
Animation method/coroutine playable once or loop 0 Answers
SpeedBoost won't reset : Problem with either WaitForSeconds or Coroutine (Solved) 2 Answers
CORoutine does not completely stop 1 Answer