Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 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 lifeisabeach · Nov 08, 2020 at 11:08 PM · inputupdatefixedupdateinput.getkey

Input.GetKey missing keyboard state on Update and FixedUpdate

I am trying to check at every FixedUpdate if the keyboard 'right' or 'left' key is pressed or not. On every FixedUpdate because that is where I am calculating and applying physics. The code is a simple:

 // RIGHT
 if(Input.GetKey("right"))
 {
     currentInput = currentInputEnum.RIGHT;
 }
 // LEFT
 else if(Input.GetKey("left"))
 {
     currentInput = currentInputEnum.LEFT;
 }
 // NO INPUT
 else
 {
     currentInput = currentInputEnum.NOINPUT;
 }

Sometimes I stop pressing the key but it doesn't change the currentInput variable, so it keeps a wrong state. For example, sometimes I stop pressing 'right' and am not pressing any key, but the state stays at right; the variable stays as RIGHT.

I have tried placing the code on Update as well as FixedUpdate.

I have recorded the screen with mac's keyboard viewer, and this variable exposed on Unity's editor. I can see on keyboard viewer that the key was released, but the variable stays unchanged.

If I press any key, then it updates the input variable correctly, but until then it stays on the old state. Strangely, if I move my mouse it updates the variable correctly as well, even though I am not using the mouse in the game at all.

This happens about 1 in a 100 key holding situations, enough to break the game. I have tried a lot of things but am short on ideas now. I have looked elsewhere on the code to see if there is something interfering on that, but it doesn't look like it, input is managed only on this place and this variable is set only there as well.

Any ideas on this, or how to implement a FixedUpdate key holding, continuous, detection are appreciated.

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 CodesCove · Nov 09, 2020 at 12:59 AM 0
Share

just to note: You can separate the GetKey under Update and any physic under FixedUpdate in the same class.. this preferred way. Also make sure when testing inputs the application focus stays in "Game" window..

avatar image adslitw · Nov 16, 2021 at 10:10 AM 0
Share

I think I have this too @lifeisabeach - did you ever figure out what's going on? What kind of Mac are you on?

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Xquality · Nov 09, 2020 at 12:26 AM

Doubt this will make a difference, but try this. If it doesn't fix it, then I suggest looking into the new input system or using booleans instead of enums. With the new input system, you can set up delegates which trigger certain things. I recommend it ;)

     void FixedUpdate()
     {
         currentInput = Input.GetKey("right") ? currentInputEnum.RIGHT : Input.GetKey("left") ? currentInputEnum.LEFT : currentInputEnum.NOINPUT;
     }
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 lifeisabeach · Nov 12, 2020 at 05:43 PM 0
Share

Thanks for the response @Xquality, I spent the past days trying to fix this, even went so far as implementing a basic solution with the new input system, but the same problem was happening. I then made a $$anonymous$$ac build and on the build the problem doesn't seem to happen.

It is weird because Unity's Update/FixedUpdate doesn't register the key up some times, only after I move the mouse or hit another key (2 things that have nothing to do with my inputs in the game and aren't even checked in code), then it updates the state in Unity. I am wondering if it has something to do between the $$anonymous$$ac and Unity, some kind of stress situation. Any ideas are welcome since it's a game breaking bug that I can't track yet.

avatar image
0

Answer by merctraider · Nov 09, 2020 at 07:06 AM

Maybe instead of doing else if statements you can try making them separate if statements since they aren't mutually exclusive; Players can press right and left at the same time. I tried it in fixedupdate and it works on my end.

        if (Input.GetKey(KeyCode.RightArrow))
         {
             currentInput = InputEnum.RIGHT;
 
         }
 
         if (Input.GetKey(KeyCode.LeftArrow))
         {
             currentInput = InputEnum.LEFT; 
         }
 
         Debug.Log(currentInput);
         if(!Input.GetKey(KeyCode.RightArrow) && !Input.GetKey(KeyCode.LeftArrow))
         {
             currentInput = InputEnum.NOINPUT;
         }

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 lifeisabeach · Nov 12, 2020 at 05:47 PM 0
Share

Thanks for the response @merctraider, you are right about the player pressing right and left at the same time, I actually check for that in code. I have tried a lot of solutions to this problem in the past days without success. But I noticed that on a $$anonymous$$ac build the problem doesn't happen, only when running on editor.

This is a strange bug because Unity sometimes doesn't capture that the 'right' or 'left' key was released, only after I move the mouse or hit another key on the keyboard (even thought I am not using the mouse or any other key in the keyboard, and not even checking for those). So, now I am thinking it is something between the $$anonymous$$ac and Unity. But still worried because it's a bug I can't track and could happen on other situations, maybe. So, any other ideas are welcome as well.

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

177 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

Related Questions

Supplying Input from Update To FixedUpdate 1 Answer

Mouse Input for RayCast - Update or FixedUpdate 1 Answer

So what's the deal with FixedUpdate? 2 Answers

How do I pass an input in Update() to FixedUpdate() for Physics movement? 2 Answers

Jumping randomly doesn't work 2 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