- Home /
My space bar refuses to work
Unity Version: 2021.1.7f1 OS: Windows 10
I'm currently making a small techno demo/foundation for my future project, where a player would drive a car. All the inputs so far are perfect, but the space bar. I plan on using the space bar as a key to activate a hand break. I wrote this code to emulate that:
void Update()
// This section of the code simulates the hand break
if (Input.GetKeyDown("space"))
{
RRWheel.brakeTorque = 200000.0f;
RLWheel.brakeTorque = 200000.0f;
Debug.log("The space bar is hit");
}
if (Input.GetKeyUp("space"))
{
RRWheel.brakeTorque = 0;
RLWheel.brakeTorque = 0;
}
The car doesn't stop. In fact, it accelerates for some reason. I checked the console and I saw that Unity doesn't even recognize my space bar for most of the time. I spent the next 10-15 minutes trying to figure out what the actual hell is going on by playing the game and smashing my space bar as frequently as I can. Weirdly, the unity recognized the space bar only 6-10 times. I'm very confused. Am I dumb?
UPD: I changed a couple of things: - Replaced "space" with Keycode.Space (didn't fix the issue) - Replaced GetKeyDown with GetKey, removed GetKeyUp completely (didn't fix the issue) - Moved the entire if statement into a separate method. And it.. worked?
Have you tested that code in a separate script in an empty scene? GetKeyUp() and GetKeyDown() work properly when used from Update(). Replacing with GetKey() means that you will receive multiple hits along consecutive frames.
Check out the example code here, it just works: https://docs.unity3d.com/ScriptReference/KeyCode.html
Answer by Edy · Aug 05, 2021 at 10:48 AM
I'd try replacing "space" with Keycode.Space in the input.GetKey methods. See: https://docs.unity3d.com/ScriptReference/KeyCode.html
What exactly is the problem with the string versions? I use them all the time and they work perfectly fine....in fact i find them easier to use compared to keycode
Easier to make a mistake (eg. not typing the key name right)
More cpu expensive - has to lookup and translate the string to keycode, every time
No advantage compared to the KeyCode version
Your answer
![](https://koobas.hobune.stream/wayback/20220613041226im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Wheel collider physics - something I'm missing? 0 Answers
My player's movement is slippery 1 Answer
How does wheelcollider mass factor into rigidbody mass? 1 Answer
Creating a simple wheel 0 Answers