- Home /
How to achieve this Key behavior
Hi, in many games after you pressed, for example, space key (for dash) it will actually remain pressed for some duration. So, if you currently in attack animation and still pressed space for a dash, and you will still perform dash after this attack animation.
So my question is - what is the best approach to create this behavior? I can simply create some bools for keys with timer time on them. Or is it possible to mimic this with some Unity's core stuff, like Input control settings?
Answer by TreyH · Jan 22, 2019 at 08:32 PM
It's usually not a feature of the input system (the button isn't "still pressed"), but a system where user inputs are queued up.
If you've played or watched Hearthstone (a game made with Unity btw), then you've probably noticed how players can perform several quick actions before their animations / results are shown on the board. In Hearthstone's case, those actions happen instantaneously behind the scenes with the on-screen representations being queued up.
So you would achieve this by setting some tolerance window (we'll say 1 second). So, even though there are actions that might restrict input (like an attack in your example), you would allow inputs when that input lockout duration was less than your tolerance.
So:
-- input queue tolerance of 1 second
-- character attacks, a 3 second animation
-- inputs are locked for the first 2 seconds
-- inputs are allowed during that last 1 second
-- the first recognized command (a dash) is queued up somehow
-- once the full 3 seconds have ended, the queued dash happens
Thx, I understand the technique. But what is the best way to set up this queue tolerance for controls?
That will depend on how your project defines an "action". As it's natural to define an action as some set of instructions to occur over time, you might do something like keep a coroutine's IEnumerator queued and run it once the current action is finished. There isn't really a single answer though as there are so many ways to tackle the problem.