Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 LokiTheCreator · May 02, 2020 at 11:13 PM · updatefixedupdategetkeydown

GetKeyDown Space doesn't work correctly with FixedUpdate

I have private string _SPACEKEY = "spacekey" and string _buttonPressed. In Update() I catch user input like this

 if (Input.GetKeyDown(KeyCode.Space))
         {
             _buttonPressed = _SPACEKEY;
         }

And in FixedUpdate() I try to make player jump like this

     if (_buttonPressed == _SPACEKEY)
     {
         _playerRigidBody.AddForce(new Vector2(_playerRigidBody.velocity.x, _playerSpeed * 1.5f), ForceMode2D.Impulse);
     }
     else //need this to stop player movement when A or D not pressed
     {
         _playerRigidBody.velocity = new Vector2(0, _playerRigidBody.velocity.y);
     }

But that doesn't work the way it supposed to. Player doesn't jump each time I press Space. I tried to Log "if" statement in FixedUpdate and it works about 1 time out of ten when I spam Space button. I tried to put bool in GetKeyDown (like jump = true) but it didn't work neither. Works perfectly if I place Jump logic in Update() but I don't want to do that. What's the way to solve this?

Comment
Add comment
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

2 Replies

· Add your reply
  • Sort: 
avatar image
5
Best Answer

Answer by Bunny83 · May 02, 2020 at 11:49 PM

As @Cassos already said any XXXDown or XXXUp method from the Input class does not work reliable in FixedUpdate. The input is evaluated at the beginning of a frame. A "Down" or "Up" event is only true for a single frame. Since FixedUpdate does not necessarily run every frame (if the visual framerate is higher than the fixed update rate) you will miss some frames. Likewise if your visual framerate is lower than the fixed update rate you may detect the event twice.


In general do any input event detection in Update. Just do

 void Update()
 {
     if (Input.GetKeyDown(KeyCode.Space))
     {
         _playerRigidBody.AddForce(new Vector2(_playerRigidBody.velocity.x, _playerSpeed * 1.5f), ForceMode2D.Impulse);
     }
 }

Note that impulse forces do not have to be applied in FixedUpdate. Only continuous forces should be applied in FixedUpdate.

Comment
Add comment · Show 1 · 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 Sejun777 · Feb 11 at 03:45 PM 0
Share

Um, I'm kind of having the same problem but is there an alternative way that I can use space key without skipping frame?

avatar image
6

Answer by Cassos · May 02, 2020 at 11:22 PM

Input.GetKeyDown doesnt work in FixedUpdate() cause sometimes it skips the frame at which u pressed the key.

Comment
Add comment · Show 1 · 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 Cassos · May 02, 2020 at 11:27 PM 1
Share

And try using:

              _buttonPressed = Input.GetKeyDown(KeyCode.Space);
 

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

133 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

Related Questions

Placing Input.GetkeyDown() inside FixedUpdate() slows down the rate I can fire at 1 Answer

Interpolation with jumping - Acting Weird. 1 Answer

Why Do Inputs Have to Been in Update (Rather than FixedUpdate)? 3 Answers

FixedUpdate limits: consistant 0.01 s on mobile devices? 1 Answer

Use frame time for physics update: is it ok? 1 Answer


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