- Home /
How to create an Idle counter ?
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!
Answer by Mmmpies · Dec 29, 2014 at 12:03 PM
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.
Thank for reply. how to find "no input set" ? That's what i want to know.
just use an if statement to capture which input button is being pressed
if(Input.GetButtonDown("Button1")
{
// do input stuff and set
theCounter = counterStart;
}
else if (Input.GetButtonDown("Button2")
{
// do input stuff and set
theCounter = counterStart;
}
else if (Input.GetButtonDown("Button3")
{
// do input stuff and set
theCounter = counterStart;
}
else
{
theCounter = theCounter - Time.deltaTime;
}
Answer by danvalho · Dec 30, 2014 at 08:11 PM
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
}
}
Your answer
Follow this Question
Related Questions
Problem with player movements 1 Answer
How to make specific text in a string array Bold C# 2 Answers
Movement Script Help 1 Answer
C# Plane Detecting a Gameobject 1 Answer