Hope word “censusing” is used fine here, english isn’t my native language. How are you guys? I’m wondering how can I census how many time or frames passed since the user is pushing a button. I’ve tried few approachs, but this is becoming tricky. I’m thinking if exist an easy way to achieve this. The point is that I need to do differents things if the user just push the button once, and by the other side if the user maintain the button pushed for a fixed amount of time/frames. Thanks for your time.
You’re trying to see how long a user held a button down? Like a GUI button or a keyboard key?
Yes sir!
That didn’t answer my question…a GUI button or a keyboard key?
GUI button sir.
float timeHeld;
if (GUI.RepeatButton(...))
{
timeHeld += Time.deltaTime;
}
else
{
timeHeld = 0;
}
I’ve did that before post. If you test it you’ll see that “else” is executed in the meantime you are pushing the button. Not a solution.
This is happening because OnGUI is called twice: once in “Layout” mode, once in “Repaint” mode. In “Layout” mode, RepeatButton will always return false.
Try this:
float timeHeld;
if (GUI.RepeatButton(...))
{
timeHeld += Time.deltaTime;
}
else
{
if (Event.current.type == EventType.Repaint) timeHeld = 0;
}
Should work, very good point StarManta. I’m wondering, is that said in manual? Or it is something you discovered by your own?
“This means that your OnGUI implementation might be called several times per frame (one call per event). For more information on GUI events see the Event reference.”
Awesome, on API, thanks. I should read the entire API.