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 FederalRazer89 · Apr 21, 2021 at 07:05 PM · scripting probleminput.getkey

Input.GetKeyUp Don't work to detect release of key

I have searched and seen several answers, but i cant get it to work. The code wont print the button up message, so i assume that i am missing something.


I am trying to make a function for Interaction and when you release the button it should confirm your choice of action. Similar to a weapon selection wheel.


Update

     void Update()
     {
         switch (isAiUnit)
         {
             case true:
                 {
                     AiInput();
                 }
                 break;
             case false:
                 {
                     PlayerInput();
                 }
                 break;
         }
     }



     void PlayerInput()
     {
 
 
         if(Input.GetKey(KeyCode.E))
         {
             if(Input.GetKeyDown(KeyCode.E))
             {
                 interactionTimeStart = Time.time;
             }            
             InteractionInput(interactionTimeStart);
 
         }
 
     }


     void InteractionInput(float timeStart)
     {
         Debug.Log(" Button Down " + (Time.time - timeStart));
         if (Input.GetKeyUp(KeyCode.E) && ((Time.time - timeStart) > 0.25f))
         {
             Debug.Log(" ButtonUp ");
         }
 
     }


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
0
Best Answer

Answer by HellsHand · Apr 21, 2021 at 08:02 PM

In your check you are using & which is a bitwise operator, use && instead.

Comment
Add comment · Show 3 · 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 FederalRazer89 · Apr 21, 2021 at 08:13 PM 0
Share

Tried it, did not work. But thanks for the input

avatar image HellsHand FederalRazer89 · Apr 21, 2021 at 08:26 PM 0
Share

Ok with that the next step I would try is removing the Time.time - timeStart just to check if it's actually KeyUp that's an issue. Though looking again, InteractionInput is being called during GetKey, this most likely will never work since GetKeyUp is only true on the frame the key was released and if the frame reads GetKey I don't believe it will get the key co$$anonymous$$g up in the same frame. Something you could test is change the key for GetKeyUp and see if it triggers while holding e

Try this:

  void PlayerInput()
  {
      if(Input.GetKeyDown(KeyCode.E))
      {
          interactionTimeStart = Time.time;
      }            
      else if (Input.GetKeyUp(KeyCode.E))
      {
          InteractionInput(interactionTimeStart);
      }
  }

then

  void InteractionInput(float timeStart)
  {
      Debug.Log(" Button Down " + (Time.time - timeStart));
      if (Time.time - timeStart) > 0.25f)
      {
          Debug.Log(" ButtonUp ");
      }
  }
avatar image FederalRazer89 HellsHand · Apr 21, 2021 at 10:00 PM 0
Share

That works, Thanks

avatar image
2

Answer by unity_ek98vnTRplGj8Q · Apr 21, 2021 at 08:42 PM

You are calling InteractionInput() from inside of the brackets around if(Input.GetKey()). On the frame that the button is released, however, Input.GetKey() will be false so InteractionInput() will never be called. You need to check for GetKeyUp outside of GetKey

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

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

211 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

Related Questions

I keep getting the "A namespace does not directly contain members such as fields or methods" error. Can someone please tell me how to fix this error? 0 Answers

How to load directory of files into a script and read them as TextAsset objects? 0 Answers

Fight game with punch and kick using keyboard inputs. 1 Answer

Enemy doesn't avoid static objects 2 Answers

Check app is installed or not in ios device with bundle id 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