Detecting if 2 Buttons are pressed at the same time

I am trying to make it so that you are not able to press more then one button at a time. If you do I want to put be able to detect it and have a bool saying multiple buttons are pressed. I have it set up so that if more then one button is pushed the character doesn’t move. That is done by

if (input.getkeydown(Keycode.uparrow) && !input.getkeydown(Keycode.Rightarrow)){
 //movement code
}

That is just a portion of the code, showing how I did it, I have more buttons and all but one has a !. I would like to have a simple bool that is can just put into an if statement if possible.

Well, you could put this in your Update(). There is surely a better way, but it’s the first that came into my mind.

int count = 0;
bool tooManyKey = false;
foreach (KeyCode kc in System.Enum.GetValues(typeof(KeyCode))
{
  if (Input.GetKeyDown(kc))
    count++;
  if (count>1)
  {
    tooManyKey = true;
    break;
  }
}

I don’t know why do you need that, but maybe you should better use GetKey instead of GetKeyDown. If you use GetKeyDown, the bool will turn true only if you simultaneously press 2 buttons, not if you hold one and then press another. It’s your choice to make x)

A big question here is “why?”. You usually want to allow the player to press several buttons at once. Also, GetButtonDown only registers on the frame the button is pressed - which means that two of them being true only happens if the player manages to press them approximately at the same time. That being said, this is not very difficult to code up:

bool rightPressed = Input.GetKey(KeyCode.RightArrow);
bool leftPressed = Input.GetKey(KeyCode.LeftArrow); 
bool upPressed = Input.GetKey(KeyCode.UpArrow);
bool downPressed = Input.GetKey(KeyCode.DownArrow);

int numPressed = 0;
if (rightPressed)
    numPressed++;
if (leftPressed)
    numPressed++;
if (upPressed)
    numPressed++;
if (downPressed)
    numPressed++;

if (numPressed == 1) {
    if (upPressed) {
        MoveUp();
    }
    ...
}

Assuming the keys are known, I would use a static bool to gate the input logic.

    public class Keys : MonoBehaviour
    {
        public static bool SomeKeyIsDown;

        void Update()
        {
            if (!SomeKeyIsDown && Input.GetKeyDown(KeyCode.LeftArrow))
            {
                SomeKeyIsDown = true;
                transform.position = (transform.position + Vector3.left*Time.deltaTime);
            }

            if (!SomeKeyIsDown && Input.GetKeyDown(KeyCode.RightArrow))
            {
                SomeKeyIsDown = true;
                transform.position = (transform.position + Vector3.right*Time.deltaTime);
            }

            // etc...

            else SomeKeyIsDown = false;

            }
        }
    }

You could also make a static method to control the bool if you didn’t want to toggle it in each input condition.

public class KeyCheck : MonoBehaviour
{
    public static bool SomeKeyIsDown;

    void Update()
    {
        SomeKeyIsDown = 
            Input.GetKeyDown(KeyCode.RightArrow) | 
            Input.GetKeyDown(KeyCode.LeftArrow) |
            Input.GetKeyDown(KeyCode.UpArrow) |
            Input.GetKeyDown(KeyCode.DownArrow);
        // etc...
    }
}

I guess there are problems with doing this exactly as is, but you get the idea.