- Home /
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.
Got the same issue in Unity 4. Space+Up+Left doesn't work. Ctrl+Up+Left works fine.
I have recently encountered this same problem with the arrows and insert key. Not sure why there would be a two key limit. Anyone find out for sure what is going on here?
Incredible! It took 7 years from this issue and I found a similar bug in A / D + S + C. Hi UNITY 2018!
Answer by Lex · Jan 05, 2010 at 01:59 PM
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.
Grr, I'm using Space with the same problem. Thanks for mentioning it 4 years ago. :)
Almost 9 years later, and it's still an issue! LOL! Changed my fire button to the "a" key. Smh, it's silly things like this that tempt me to build my own game engine, lol.
and still now, almost 11 years later.... I have the same problem. still thanks for pointing it out tho. I really couldnt find the cause of me randomly jumping every time. thanks! :)
Answer by joe · Jan 04, 2010 at 07:03 PM
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.
$$anonymous$$y keyboard limit is 4 keystrokes anywhere else. :<
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 )
Answer by Lex · Jan 04, 2010 at 07:01 PM
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
}
Answer by Ricky Helgesson · May 11, 2010 at 08:37 AM
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
Answer by kweiko · Oct 22, 2010 at 12:29 AM
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
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.. I$$anonymous$$O it's a Unity bug.
Your answer
Follow this Question
Related Questions
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
Update - Delaying until next frame? 0 Answers
Simple (?) touch-command on iPhone 2 Answers
Mecanim character wobbles when changing camera relative direction. 0 Answers
how to integrate steering wheel controller for ps3 in unity?? 0 Answers