Nick
Nick

Reputation: 1891

Key no longer considered "down" after another key is pressed

I am attempting to make a simple third person camera rotate around my character. I want the X key to rotate right and the Z key to rotate left. I have this working. My issue is, when I have one of the keys pressed and held (say X), then press any other key, Input.GetKey(KeyCode.X) stops returning true. So for the following simple example, it would print the message until another key is pressed, then will not again until I re-press the key.

void LateUpdate()
{
    if(Input.GetKey(KeyCode.X))
    {
        print("X is down"); 
    }
}

This prevents me from being able to rotate the camera while moving my character, for as soon as I attempt to change directions via an arrow key, it renders my if statement false. Is there another method I should be using?

Edit - This is not an issue with Unity or my code. I was using Teamviewer, which must handle the inputs differently

Upvotes: 1

Views: 113

Answers (3)

Nick
Nick

Reputation: 1891

It ended up being due to remote access. I was using Teamviewer to remote into the laptop that was running Unity. Once I used the laptop directly it worked as expected.

Upvotes: 0

StackOverthrow
StackOverthrow

Reputation: 1284

This could be a hardware issue. Different quality levels of keyboards have different behavior. For example, I once had a keyboard where if I pressed more than three keys at the same time, it would not register when they were released, and my character would be stuck running or spinning or whatever until I pressed and released the key again. Expensive "gaming" keyboards should handle multiple simultaneous keypresses better than others. YMMV.

Upvotes: 0

Ali Kanat
Ali Kanat

Reputation: 1889

This works for me. Could it be you are using else if while checking if the button is pressed?

    if (Input.GetKey(KeyCode.X))
    {
        print("X is down");
    }
    if (Input.GetKey(KeyCode.Y))
    {
        print("Y is down");
    }

It also works for me if i have only 1 if. Even if i press any other key "X is down" is printed.

Upvotes: 1

Related Questions