Hello.
Before reading please note that the two animation clips that I will be talking about in here are the ones with the name of “Start” (or working) which is looped and “Stop” (non looped).
I have an animator that has a looped animation clip in it, I know how to start and how to stop the animation clip but I want to start and stop the animation every n seconds, (let say 2 seconds), I used an IEnumerator in this way:
using UnityEngine;
using System.Collections;
public class AnimController : MonoBehaviour
{
public GameObject w; //Gameobject that has the animator controller
int Bool1;
int phase;
bool temp = true; //A bool that controls the stop/play animations
void Start()
{
Bool1 = Animator.StringToHash("IsOn"); //Simplified, I've used something else in this line but I guess you've got what this line means
phase = 0; //Determine whether the object is off or on, if on play animation if off stop playing that
}
void FixedUpdate() //Have used Update() before,there are not different in this case
{
//some codes deleted due to simplicity and clearance
if (phase == 1)
{
StartCoroutine(Check());
if (temp)
{
w.GetComponent<Animator>().SetBool(Bool0, true); //This Bool controls the state machine if false play a non looped single frame animation so that it looks like that the animation is stopped, otherwise plays an animation.
}
else
{
w.GetComponent<Animator>().SetBool(Bool0, false);
}
}
}
}
IEnumerator Check()
{
temp = !temp;
yield return new WaitForSeconds(2f);
}
}
But this is not accurate, sometimes it takes more and some times takes less than the n seconds that was defined.
If you know the answer in JS only, then no problem just share your answer in the language. else if you know anything related to this please share it here ASAP.
I am waiting for an answer.
Thanks for your attention.