x


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.

more ▼

asked Jan 04 '10 at 02:05 PM

Lex gravatar image

Lex
173 8 9 16

the problem is in other parts of your code

Jan 04 '10 at 03:04 PM Ashkan_gc

Could you enlighten me then? Any guesses?

Jan 04 '10 at 05:56 PM Lex
(comments are locked)
10|3000 characters needed characters left

5 answers: sort oldest

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
}
more ▼

answered Jan 04 '10 at 07:01 PM

Lex gravatar image

Lex
173 8 9 16

(comments are locked)
10|3000 characters needed characters left

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.

more ▼

answered Jan 04 '10 at 07:03 PM

joe gravatar image

joe
1 1

My keyboard limit is 4 keystrokes anywhere else. :<

Jan 04 '10 at 07:05 PM Lex

If that DOES occur you should hear your computer go "BEEP! BEEP! BEEP!"

99/100 times it's the handling, not the hardware, that can handle all the keys you need for single player games applications or other software ( which makes sense, since nobody would buy a keyboard that works only with 80% of the programs out there )

Oct 29 '10 at 09:14 AM Proclyon
(comments are locked)
10|3000 characters needed characters left

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.

more ▼

answered Jan 05 '10 at 01:59 PM

Lex gravatar image

Lex
173 8 9 16

(comments are locked)
10|3000 characters needed characters left

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

more ▼

answered May 11 '10 at 08:37 AM

Ricky Helgesson gravatar image

Ricky Helgesson
59 1

(comments are locked)
10|3000 characters needed characters left

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

more ▼

answered Oct 22 '10 at 12:29 AM

kweiko gravatar image

kweiko
1 1 1 1

I have the same issue with Unity 4 and 3 on different projects, different machines and different keyboards! I noticed, that it does not work with the left arrow key. When I hold down for example the left and up arrow keys and then press another arbitrary button, the third one will not be tracked. When I press two buttons and then the left arrow key, the left arrow is not tracked. However, everything works fine when I use WASD + other keys.. IMO it's a Unity bug.

Dec 05 '12 at 07:46 PM hicks
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x5088
x955

asked: Jan 04 '10 at 02:05 PM

Seen: 2699 times

Last Updated: Dec 05 '12 at 07:46 PM