- Home /
How to check if no buttons are pressed for a specific time?
float timeVariable = 0f;
void Update () {
if ( canCheck == true){
timeVariable = Time.time + 5;
Do{
//nothing until time has passed
}while ( Time.time < timeVariable && Input.AnyKey == false)
If (Time.time >= timeVariable){
//do stuff
}
Am I wrong or what? Without the Do/While, I can tell that timeVariable is always the same as Time.time, So how do I do this?
I've tried that. When I use the Do/While loop the Unity Editor freezes on play.
put in a coroutine, don't use while in Update, Update is called once per frame. Time don't change inside the same frame, for this reason you enter in a infinite loop and unity freezes.
Answer by JathOsh · Oct 03, 2016 at 06:00 AM
This code should hopefully fit your needs
public int time = 0;
//Use fixed update beacuase its called every fixed framerate frame
void FixedUpdate () {
if(!Input.anyKey){
//Starts counting when no button is being pressed
time = time + 1;
} else {
// If a button is being pressed restart counter to Zero
time = 0;
}
//Now after 100 frames of nothing being pressed it will do activate this if statement
if(time == 100) {
Debug.Log("100 frames passed with no input");
//Now you could set time too zero so this happens every 100 frames
time = 0;
}
}
you can use this for actual time in seconds
public float timer = 0;
void FixedUpdate () {
if(!Input.anyKey){
timer += Time.deltaTime;
} else {
timer = 0;
}
if(timer >= (TIME IN SECONDS)) {
//your code
timer = 0;
}
}
Answer by schizmo · Oct 03, 2016 at 07:46 AM
You would need to start a coroutine to increment a timer and evaluate some bools when the timer has begun and elapsed, this may not solve your problem directly but I believe the framework should help you reach a solution of your own. I also tried to comment it as best as I could for clarity, hopefully this should allow you to write your own similar code that fits within the script you're using.
using UnityEngine;
using System.Collections;
public class ButtonTimer : MonoBehaviour {
public float inputTimer; //the timer begins at 0
public float frequency; //the frequency the timer is checked, if you need to check the timer more frequently then try 0.5f or 0.25f or even 0.1f, but 1f should be sufficient.
public float timeLimit; //the limit of the timer
public bool timing;
public bool noInput;
void Start () { //none of these things need to be set at start, they can be set elsewhere if need be
inputTimer = 0f;
frequency = 1f;
timeLimit = 5f;
}
void Update () {
if (!noInput && !timing) { //first, the script makes sure the timer hasn't already begun or elapsed
if (!Input.anyKey) { //then, if there is no input
timing = true; //the timing begins
StartCoroutine ("InputTimer"); //and the coroutine is called
}
}
if (Input.anyKey) { //if input is ever detected
timing = false; //the timing ends
noInput = false; //the "noInput" status is reset (if it had been activated in the first place)
inputTimer = 0f; //the timer is also reset to 0, this is useful if the timer needs to be accessed by another portion of the script but this can be removed because the timer also sets itself to 0 at the start of the coroutine
StopCoroutine ("InputTimer"); //and the coroutine is ended
}
}
IEnumerator InputTimer () {
inputTimer = 0f; //timer resets at the start of the coroutine
while (timing) { //only workins while timing
while (inputTimer < timeLimit) { //while the timer is below the timeLimit
inputTimer += frequency; //the timer counts upward at the same rate it is checked
yield return new WaitForSeconds (frequency); // 1 second is sufficient but it can check every half or quarter or whatever you need
}
if (inputTimer >= timeLimit) { // once the timer has exceeded the timeLimit
noInput = true; //it tells the script that there has been no input
timing = false; //stops timing (because there's no need once it has determined there's no input)
Debug.Log ("No input!"); //I just left this here while testing
yield return noInput;
}
}
}
}
EDIT: I forgot to mention it initially, but this only works for key and button inputs. Mouse movement is not counted.
Your answer
![](https://koobas.hobune.stream/wayback/20220612093859im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Game Timer help needed 2 Answers
How to make a timer read to the .001 of a second 2 Answers
Minute Timer Issue 1 Answer
Timer running down too quickly 1 Answer
how to make a varied countdown timer 1 Answer