When pressing two keys and then pressing a third key, all without releasing, Input.GetKeyDown does not know that the third key has been pressed.
Here is my code:
public class Keys
{
internal bool leftPressed = false;
internal bool rightPressed = false;
internal bool upPressed = false;
internal bool downPressed = false;
}
public Keys keys = new Keys();
void Start () {
}
void Update ()
{
updateButton(KeyCode.LeftArrow);
updateButton(KeyCode.RightArrow);
updateButton(KeyCode.UpArrow);
updateButton(KeyCode.DownArrow);
}
void updateButton(KeyCode key)
{
if (Input.GetKeyDown(key))
{
if (key == KeyCode.LeftArrow)
keys.leftPressed = true;
if (key == KeyCode.RightArrow)
keys.rightPressed = true;
if (key == KeyCode.UpArrow)
keys.upPressed = true;
if (key == KeyCode.DownArrow)
keys.downPressed = true;
}
else if (Input.GetKeyUp(key))
{
if (key == KeyCode.LeftArrow)
keys.leftPressed = false;
if (key == KeyCode.RightArrow)
keys.rightPressed = false;
if (key == KeyCode.UpArrow)
keys.upPressed = false;
if (key == KeyCode.DownArrow)
keys.downPressed = false;
}
}
All I wanted to do was have a player class move a gameobject when a key was pressed down. If both directions were pressed they would cancel each other out and set the player's velocity to 0.
Hitting left and right at the same time does so, but trying to press up or down doesn't change the up or downs value at that point. It's as though Input.GetKeyDown only contained 2 values and not the whole keyboard keys states?
Note: even pressing two random keys on the keyboard would stop left, down, up or right from changing values.
Does anyone have a suggestion or a fix to my code? Thanks,