Multiples keys at the same time issue.

I have this code in C# to control my Rigidbody sphere character:

if (m_bGrounded)
    {
        //
        // Initialize fX and fZ based on controls pressed by players
        //
        float fX = Input.GetAxis("Horizontal") * Time.deltaTime * Speed * 100;
        float fZ = Input.GetAxis("Vertical") * Time.deltaTime * Speed * 100;

        //...

        // Jump
        if (Input.GetButton("Jump"))
        {
            //...
        }

    }

Problem is that character won't jump if I press space while holding 2 axis nor move left if I'm holding UP and SPACE and then press LEFT for example.

I think there's a kind of buffer for keys but 2 keys seems to be a little too small so I'm wondering what am I doing wrong.

There are limits to how many keys the keyboard itself will actually keep track of, so it might just be the keyboard, and not Unity or your code.

(Game controllers don't seem to have this issue, only keyboards do. I don't know if the problem is the hardware, drivers, or OS, but its a common problem playing any game with a keyboard.)

I'm not sure if you are actually using enough keys to be hitting that limit. A good way to test it though is by trying other games using keyboard controls, and see if the control stops responding well when holding high numbers of keys at the same time.

Problem solved, you wouldn't believe if I said that the SPACE key was the problem. I still don't know why it won't work with SPACE but it works fine with any other key I tested.

Maybe SPACE is a kind of special key or just a bug... I don't know... I checked input options and tried either GetButton() and GetKey() for SPACE and it's definetly the key but it works fine with the character controller though...

If anyone out there knows the reason please answer here because I still want to jump with the SPACE key as it's a kind of universal key for jumping.

Whole code for movements for my Rigidbody-Sphere-Character that moves on a 45 degrees axis (don't mind the portuguese comments):

using UnityEngine;
using System.Collections;

public class Movement : MonoBehaviour
{
    #region Atributos

    private bool m_bGrounded;

    #endregion

    #region Propriedades

    public float Speed = 5;

    public float Jump = 3;

    public float Gravity = 10;

    #endregion

    #region Mtodos

    void Awake()
    {
        rigidbody.useGravity = false;
        m_bGrounded = false;
    }

    /// <summary>
    /// Atualizao do comportamento
    /// </summary>
    void FixedUpdate()
    {
        if (m_bGrounded)
        {
            //
            // Inicializa os valores fX e fZ baseando-se nos controles pressionados pelo jogador
            // aplicando a velocidade definida
            //
            float fX = Input.GetAxis("Horizontal") * Time.deltaTime * Speed * 100;
            float fZ = Input.GetAxis("Vertical") * Time.deltaTime * Speed * 100;

            //
            // Aplica as foras ao rigidbody do objeto
            //
            rigidbody.AddForce(fZ * Mathf.Cos(Mathf.PI / 4), 0, fZ * Mathf.Sin(Mathf.PI / 4));
            rigidbody.AddForce(fX * Mathf.Sin(3 * Mathf.PI / 4), 0, fX * Mathf.Cos(3 * Mathf.PI / 4));

            // Jump
            if (Input.GetButton("Jump"))
            {                
                rigidbody.velocity = new Vector3(rigidbody.velocity.x, CalculateJumpVerticalSpeed(), rigidbody.velocity.z);
            }

        }
        else
        {
            //
            // Inicializa os valores fX e fZ baseando-se nos controles pressionados pelo jogador
            // aplicando a velocidade definida
            //
            float fX = Input.GetAxis("Horizontal") * Time.deltaTime * Speed * 25;
            float fZ = Input.GetAxis("Vertical") * Time.deltaTime * Speed * 25;

            //
            // Aplica as foras ao rigidbody do objeto
            //
            rigidbody.AddForce(fZ * Mathf.Cos(Mathf.PI / 4), 0, fZ * Mathf.Sin(Mathf.PI / 4));
            rigidbody.AddForce(fX * Mathf.Sin(3 * Mathf.PI / 4), 0, fX * Mathf.Cos(3 * Mathf.PI / 4));
        }

        rigidbody.AddForce(new Vector3 (0, -Gravity * rigidbody.mass, 0));

        m_bGrounded = false;
    }

    /// <summary>
    /// Mtodo para quando o objeto est em contato
    /// </summary>
    /// <param name="collisionInfo">Informao das colises</param>
    void OnCollisionStay(Collision collisionInfo)
    {

        foreach (ContactPoint contactPoint in collisionInfo.contacts)
        {
            //
            // Se alguma delas for normal no eixo Z
            //
            if (contactPoint.normal == new Vector3(0, 1, 0))
            {
                //
                // Est no cho
                //
                m_bGrounded = true;
            }
        }
    }

    private float CalculateJumpVerticalSpeed()
    {
        // From the jump height and gravity we deduce the upwards speed 
        // for the character to reach at the apex.
        return Mathf.Sqrt(2 * Jump * Gravity);
    }

    #endregion
}

This is likely some kind of bug. I have the same issue with left ctrl also. If I drive my vehicle around and press forward and steer right (w and d) and press space or ctrl at the same, the key is not read. This works fine when playing e.g. BattleField on the same computer.

I think that the Unity guys should look into this. Something is not working properly.

/Ricky

Hello, any updates on this issue? How did you work around it? I really want to use the space button with the arrow keys :(

Thanks in advance! /Kweiko

Got the same issue in Unity 4. Space+Up+Left doesn’t work. Ctrl+Up+Left works fine.

I am late to the game, but I’ll post anyway.

I had the same problem; I had two players flying spaceships using the keyboard.
With some combinations of keys, other key presses would be ignored.

I was using the CTRL and ALT keys.
When I used “normal” letter keys, the problem went away.

– Paul

if (Input.GetKey (KeyCode.W) && Input.GetKey (KeyCode.LeftShift))
{
//Do your stuff here.
}

This is for when you are sprinting. (2 buttons)

if (Input.GetKey (KeyCode.W) && Input.GetKey (KeyCode.LeftShift) && Input.GetKey (KeyCode.A))
{
//Do your stuff here.
}

This is for when you are sprinting and moving left at the same time. (3 buttons)

Incredible! It took 7 years from this issue and I found a similar bug in A / D + S + C. Hi UNITY 2018!

You cant really pin most multi-key issues on Unity. The truth is that most (used to be all) keyboard work on a grid or Matrix of Diodes and the way it is constructed causes a physical limitation on which signals may pass when multiple keys are pressed. Its encoding on a hardware level. This is the whole reason why Games moved over from classic Arrow-Key movement to WSAD, nothing to do with the feel of it…everything do to with Key Response and prevention of key blocking.

If a keyboard is designed differently it may show different results. More expensive USB keyboard may circumvent this by design but cheaper ones will still have this problem.

I’m having a similar issue where the user can’t press 4 keys at the same time. If they press w, s, up, and down, one of the keys doesn’t register. The game works fine with a controller using the bumpers and triggers.