- Home /
Checking if a key has been pressed twice in quick succession
Hi guys,
My game is 2.5d, you control one character, he can move left right and jump. The controls are WASD and Spacebar. What I'm trying to do is have it so that when the user presses DD or AA in quick succession it performs a dash (increases speed for about half a second)
So heres my current logic of the idea for D:
thanks for any feedback
I've updated my original post now with a more solid bit of code, the pastebin link - please have a look :D
@benhacking: Use comments!!!
Your post isn't an answer. So use "add new comment"
Answer by felixpk · Aug 27, 2016 at 10:07 PM
I really noticed the same effect as @benhacking mentioned in the comments, so I played around a little bit and found out a nice little solution.
if(Input.GetKeyDown(KeyCode.A) && firstButtonPressed) {
if(Time.time - timeOfFirstButton < 0.5f) {
Debug.Log("DoubleClicked");
} else {
Debug.Log("Too late");
}
reset = true;
}
if(Input.GetKeyDown(KeyCode.A) && !firstButtonPressed) {
firstButtonPressed = true;
timeOfFirstButton = Time.time;
}
if(reset) {
firstButtonPressed = false;
reset = false;
}
It is interesting how the order of the if-statements is critical for this solution to work.
I'm surprised that no one else found this useful? I had tried many variations and this one worked best.
I used a slight variation of this, as triple tapping triggered it twice. This is my code:
if(Input.GetButtonDown("Jump") && doubleTap) {
if(Time.time - doubleTapTime < 0.4f) {
Debug.Log("Double-tapped");
doubleTapTime = 0f;
}
doubleTap = false;
}
if(Input.GetButtonDown("Jump") && !doubleTap) {
doubleTap = true;
doubleTapTime = Time.time;
}
I expanded on your solution to log a triple tap. I did a lot of searching and yours was the closest to what I was looking for. There's probably some more optimization potential here, like using a switch.
/* Detect double and triple tap for Jump */
if (Input.GetButtonDown("Jump"))
{
if (tripleTap) {
if ((Time.time - tripleTapTime) < tapResetDelay)
{
Debug.Log("Triple-tapped");
tripleTapTime = 0f;
}
tripleTap = false;
}
if (doubleTap && !tripleTap) {
if ((Time.time - doubleTapTime) < tapResetDelay)
{
Debug.Log("Double-tapped");
doubleTapTime = 0f;
tripleTap = true;
tripleTapTime = Time.time;
}
doubleTap = false;
}
if (!doubleTap && !tripleTap) {
doubleTap = true;
doubleTapTime = Time.time;
}
}
Answer by numberkruncher · Mar 28, 2012 at 12:52 PM
I would do this in a different way, (code not tested but should work):
public class MyBehaviour : MonoBehaviour {
// Must double tap within half a second (by default)
public float doubleTapTime = 0.5f;
// Time to wait between dashes
public float dashWaitTime = 2.0f;
// Time that the dash button was last pressed
private float _lastDashButtonTime;
// Time of the last dash
private float _lastDashTime;
void Update() {
if (isDashPossible && Input.GetKeyDown(KeyCode.D)) {
// If second time pressed?
if (Time.time - _lastDashButtonTime < doubleTapTime)
DoDoubleDash();
_lastDashButtonTime = Time.time;
}
}
bool isDashPossible {
get {
return Time.time - _lastDashTime > dashWaitTime;
}
}
void DoDoubleDash() {
_lastDashTime = Time.time;
// blah...
}
}
nice, that looks really promising, will have a go at implementing it :D thanks
it turns out getkeyup is necessary so i'm integrating that at the moment
Cool! getkeyup would give a slightly slower response time for the player than getkeydown but obviously that depends upon what your trying to achieve
the reason it is necessary is that if im holding D down, each update it thinks i've pressed it again, this is bad because i don't want the user to be able to HOLD D then release and quickly press D once to claim the double tap, they must press the first D and release it just as fast (or there abouts) as the second D.
For this reason i need to check the time between keydown and keyup on the first D
That should not be the case, the down value is only true for the frame when the button went down. There are three alternatives Get$$anonymous$$eyDown (gone down in current frame) Get$$anonymous$$ey (whether its just gone down or not) and Get$$anonymous$$eyUp (gone up in current frame) http://unity3d.com/support/documentation/ScriptReference/Input.Get$$anonymous$$eyDown.html
Answer by by0log1c · Mar 27, 2012 at 07:03 PM
It might not be the best implementation but this is pretty much the way I handle similar cases. I've wrote this for a dash and while its a little longer than I'd have expected, it works fairly well.
float maxDelay = 0.5f;
bool dashReady = false;
void Update()
{
if(Input.GetKeyDown(KeyCode.Space))
{
if(dashReady)
Dash();
else
PrepareDash(true);
}
}
void Dash()
{
dashReady = false;
//dash action goes here
}
void PrepareDash(bool makeReady)
{
//this is where the handling happens
CancelInvoke("CancelDash");
Invoke("CancelDash",maxDelay);
dashReady = true;
}
void CancelDash()
{
dashReady = false;
}