Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Legioner3421 · Aug 05, 2021 at 10:38 AM · scripting problemphysicswheelcollider

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?

Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Legioner3421 · Aug 06, 2021 at 10:21 AM 0
Share

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?

avatar image Edy Legioner3421 · Aug 06, 2021 at 10:24 AM 0
Share

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

1 Reply

· Add your reply
  • Sort: 
avatar image
2

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

Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Pangamini · Aug 05, 2021 at 11:08 AM 0
Share

I'd ban using the string version

avatar image rage_co Pangamini · Aug 05, 2021 at 01:18 PM 0
Share

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

avatar image Pangamini rage_co · Aug 05, 2021 at 01:29 PM 1
Share
  1. Easier to make a mistake (eg. not typing the key name right)

  2. More cpu expensive - has to lookup and translate the string to keycode, every time

  3. No advantage compared to the KeyCode version

Show more comments

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

282 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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

Changing wheel collider direction in Unity 5 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges