I have a tank that always animates its “Scanning/waiting” animation when no key is pressed. I want it to play its scanning animation only after a certain amount of time has passed after having not pressed a key.
So how do I measure the time elapsed after having pressed a certain key?
I’m using JavaScript for this project by the way, but I can also use C#.
I know it may have something to do with Time.time, but I can’t get my head around it.
public class time : MonoBehaviour {
public bool idle = false;
public float timeToIdle = 2.0f; //2 seconds
float currentTime = 0f;
// Use this for initialization
void Start () {
currentTime = Time.time + timeToIdle;
}
// Update is called once per frame
void Update () {
if(!idle) //this can be replaced by something to check if there is no input etc..
{
checkIdle();
}
}
void checkIdle()
{
if(Time.time > currentTime)
{
idle = true;
//run your anim here or a seperate function to translate the boolean value
currentTime = Time.time + timeToIdle;
}
}
}