Double Tap with GetAxis()

I’ve been trying to implement running, by double tapping the movement keys. The question is straightforward;
how can I implement double tapping keys which are accessed through the Input.getaxis() function?

You need two parts:

  1. To detect when a user has “pressed” a direction (that is, an equivalent to GetKeyDown but for an axis
  2. To detect when this happens twice in rapid succession
    The first part basically goes:
bool isRunning = false;
float lastPressedRight = -999f;
bool wasRightPressed = false;
void Update() {
bool isRightPressed = Input.GetAxis("Horizontal") > 0.5f;
if (isRightPressed && !wasRightPressed) {
//Do stuff (see below)
}
wasRightPressed = isRightPressed;
if (!isRightPressed) isRunning = false;
}

For the second part:

if (Time.time < lastPressedRight + 0.5f) { // half a second window for double-tapping
isRunning = true;
}
lastPressedRight = Time.time;
1 Like