Hi
If button pressed down for 3 seconds. do…what every. Here "quitTimer value not back to 0.
if(Input.GetButton("xbox a")) {
float quitTimer = Time.time;
if (quitTimer>5) {
Debug.Log ("you out !! ");
}
else {
quitTimer = 0;
}
}
Time.time continually counts up while the game is running so if you press the button at any point beyond 5 seconds after starting then you’ll hit that log. I’d use GetButtonDown to set quitTimer to 0 and then add Time.deltaTime each frame GetButton is true; then check if it’s greater than 5.
nope, something more like:
float timer;
float holdDur = 3f;
void Update()
{
if(Input.GetButtonDown("xbox a"))
{
timer = Time.time;
}
else if(Input.GetButton("xbox a"))
{
if(Time.time - timer > holdDur)
{
//by making it positive inf, we won't subsequently run this code by accident,
//since X - +inf = -inf, which is always less than holdDur
timer = float.PositiveInfinity;
//perform your action
}
}
else
{
timer = float.PositiveInfinity;
}
}
4 Likes
The PositiveInfinity bit is probably unnecessary because you can’t have a button hold without a button down.
if it’s held for longer, you’ll repeatedly process “waited 3 seconds”. You need to flag that it’s been processed. Which I use the +inf to flag.
The +inf at line 25 isn’t super necessary though, I just put it in for safe keeping. I just don’t trust that API some times.
1 Like
Thank you so much. 
[Solved]
Its late and already dead but thank you guys, This solved my problem for a racing game.
1 Like
Let me guess hold 2 just before light goes green otherwise a stall?