How to clear the keyboard buffer

Hello all,

I’m having an issue with the game that I am making. I currently have the controls set up to use both a keyboard and a gamepad.

On the joystick the shoulder buttons turn left and right, and on the keyboard, either A/D or the left/right arrow keys turn left and right.

If I hold down the shoulder buttons on the gamepad, the character will turn until the very moment that I release the button, which is great.

However, the problem I’m running into is that the longer I hold down a key on the keyboard, the longer the character will continue to still turn after I have released the key. I know when working on games in the past that this problem stemmed from the keyboard storing up keypresses in the buffer, and then continuing to register them as held until it emptied. Clearing the buffer after every read resolved the issue.

Is there a way to do that in Unity?

Here’s what’s in Project Settings → Input :

  Name:  Horizontal
  Negative Button: left
  Positive Button: right
  Alt Negative Button: a
  Alt Positive Button: d
  Gravity: 1
  Dead:  0.001
  Sensitivity:  1
  Type:  Key or Mouse Button
  Axis:  X axis

  Name:  Vertical
  Negative Button: down
  Positive Button: up
  Alt Negative Button: w
  Alt Positive Button: s
  Gravity: 1
  Dead:  0.001
  Sensitivity:  1
  Type:  Key or Mouse Button
  Axis:  Y axis

Here’s the code for my controls:

float keyboardHorizontal = Input.GetAxis ("Horizontal");
float keyboardVertical = Input.GetAxis ("Vertical");
if (buttons [6].getHeld () || keyboardHorizontal < 0) {
  if (!current.turningLeft) {
    current.turningLeft = true;
    CmdTurnShip (1);
} else {
  if (current.turningLeft) {
    current.turningLeft = false;
    CmdTurnShip (0);
if (buttons [7].getHeld () || keyboardHorizontal > 0) {
  if (!current.turningRight) {
    current.turningRight = true;
    CmdTurnShip (-1);
} else {
  if (current.turningRight) {
    current.turningRight = false;
    CmdTurnShip (0);

void CmdTurnShip (int dir) {

class Ship() {
    float angle;
    public void setTurn(int dir) {
        angle += dir;
        transform.Rotate (new Vector3 (0, 0, dir));

The way it is designed to work is for the ship to receive the signal to turn when the button is pressed. It will then continue to turn every Update() for as long as the boolean for it to turn is true (which is set in the CmdTurnShip() function). Then, when the axis is released, the code tells it to stop turning.

This works perfectly with the Joystick, but the keyboard will lag after it is released. The longer it is held, the longer it will go before the input changes. There is nothing in my code that stores presses.
If I simply print the current state of Input.GetAxis(“Horizontal”) to the console, it, too, will show that it is pressed down after I release the key.


If you’re trying to turn continuously while a key is being held down you should be using the GetKey/GetButton functions, not the GetKeyDown/GetButtonDown functions. The keyboard buffer shouldn’t be involved at all.