- Home /
Is there any function to check how long a button is pressed?
For example, let say that there is an Attack Button.
If the player make a slight tap [ immediately release after press ], then the Player will do Light Attack
a long tap [ hold for a bit then release ], then the Player will do Heavy Attack
a hold [ Not release after a certain amount of time ], then the Player will do Charging
I understand that this can be achieve by
timer++ during Update()
and
timer = 0 when buttonUp()
What I would like to know is,
Is there any Unity Input function that detect how long the button was pressed,
instead of coding a separate timer?
Answer by GTX Titan · Feb 01, 2014 at 10:45 AM
I would do this when button is pressed: timer += Time.deltaTime;
and when it's not i would do this: ... //some code timer = 0; //re-initialize
Same idea, but I think easier:
On the press set your variable downTime=Time.time;
. Then on release look at Time.time-downTime
, which is the hold-time in seconds. Essentially, write down the time they press, then look at the clock and subtract when they let go.
I didn't realize you could do that :D thx might help me someday But you should add some variable to check if it's the first time the button is down... otherwise it would record a new downTime each frame and on release you would get what Time.deltaTime would give you...
Thank you Owen, that is the best method I've seen thus far.
To GTX Titan, Unity already have ButtonDown, which will get call only once when button got pressed, so that should come in handy.
Answer by getyour411 · Feb 01, 2014 at 09:57 AM
No, there are various methods, events, timers, while/loop increments, etc but no Input.HoldDownFor()