Input.GetKey() is not resonding

So, I’m trying to use Input.GetKey() to add torque to my player object making him rotate when the arrow keys are pressed, like in the tutorial. But it’s not responding- I’ve tried using keycodes and just key names and also tried different keys, but it isn’t responding. I’ve also confirmed that the update is being called by using Debug.Log. I’m prepared to use a different method, but I’d like to understand what I’m doing wrong here in case it causes other issues. Thank you for any help!

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class playerController : MonoBehaviour
{
    [SerializeField] float torqueAmount = 1f;
    Rigidbody2D rb2d;

    // Start is called before the first frame update
    void Start()
    {
        rb2d = GetComponent<Rigidbody2D>();  
    }

    // Update is called once per frame
    void Update()
    {
        if(Input.GetKey(KeyCode.LeftArrow))
        {
            rb2d.AddTorque(torqueAmount);
            Debug.Log("left arrow key pressed");
        }
        else if(Input.GetKey(KeyCode.RightArrow))
        {
            rb2d.AddTorque(-torqueAmount);
        }
    }
}

Hi William,

Welcome back! :slight_smile:

Your code looks fine so far. Is it attached to your rocket (the root game object)? And what value does the “Torque Amount” field have in the Inspector of the rocket? Try to increase the value.

Thank you for responding! It is attached, and the value is increased to 10 in the inspector. I tried changing it to 10 in the code itself and that still didn’t work. I ran rb2d.AddTorque(torqueAmount); in the update loop outside of the if else statements and it worked then, so I can only assume it has something to do with the input or the code detecting it.

Is there anything in Unity I should show?

The rigidbody would be good to show. I tested the code and it works fine


I didn’t see anything different from what Rick did, plus it works when I tell it to turn without the if statement so I don’t think it’s the player character not being able to.

Do you not have the rotation locked? Open the constraints on the rigidbody and check that it’s not checked

it’s not

The only thing I can think of that remains is that your project is using the new input system. Go to Edit > Project Settings... and check under the Player section


This should either be set to ‘Both’ or ‘Input Manager (Old)’


it’s set to both yeah. I tried switching to Input Manager and that didn’t work either-

I don’t understand how the ‘if’ can just not work. Can you possibly upload the project somewhere so I can take a look?

Sure, I uploaded it to google drive, here’s the link: Snow Boarder - Google Drive
Let me know if there’s any issues downloading it.

Also: I tried moving it to my laptop to see if that would change anything, and to my surprise it worked there, so it might have something to do with my computer’s settings or Keyboard. I’d still like to try and find out what’s preventing it from working on my desktop in case I can’t use my laptop in the future. My laptop has a build in keyboard and my desktop uses one connected via USB. I tried uninstalling and reinstalling Unity thinking maybe something happened to my desktop’s installation, but it still didn’t work.

Yeah, so I got your project and it was working just fine. I use a laptop with a usb keyboard and had no issues. I really don’t understand.

One more thing to check (if you want) is in the Project Settings under Input Manager there should be a ‘Use Physical Keys’ checkbox. Check or Uncheck it and see if that makes any difference. I usually keep that one checked because some of my friends have Dvorak keyboards (and there’s even one with a Colemak layout) and it just helps that WASD is the same keys regardless of where the W, A, S and D keys actually are on the keyboard

Sorry for not responding, that didn’t work and I wanted to try and find a solution with the knowledge it might be my keyboard. I saw someone mentioning resizing the window on a forum, and that makes it work for me. After pressing “play”, I have to click on the game window and it’ll suddenly accept controls. I know you have to do that if you click off the window while playing for something else, but I’m pretty sure I shouldn’t have to right off the bat. This keeps getting weirder-

I found the issue. The game window was set to “play unfocused” rather than “play focused” or “play maximized”. It works perfectly now, it picks up the controls, and right away.

1 Like

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms