hello, I am new for unity. I’m trying to implement a counter that counts down whenever the player input stops, and whenever input begins again the counter is to reset. I try many code but not working. Any tips would be much appreciated. Thanks!
Mmmpies
2
Set a float e.g.
private float counterStart = 20f; // whatever value you want
and anther
private float theCounter;
In start set
theCounter = counterStart;
check for user input and if the user is inputting set
theCounter = counterStart;
if there is no input set
theCounter = theCounter - Time.deltaTime;
And check for it going below zero.
If you want only to know if any key/button was pressed, you can use Input.anyKeyDown and reset or increment the counter:
private float idleCounter = 0.0f;
void Update() {
if (Input.anyKeyDown) {
idleCounter = 0.0f; // reset counter
} else {
idleCounter += Time.deltaTime; // increment counter
}
}