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 Jurassic · Jul 19, 2013 at 08:13 PM · c#inputgetkeydown

Trying to see how long the player holds down a key. GetKeyDown & GetKeyUp behaving oddly. (C#)

I'm trying to allow the player to jump higher based on how long he holds down the jump key (to a limit). For now I'm not bothering with the limit just to make sure the code is working, and it's behaving strangely. I can't seem to figure how why it's doing what it's doing. This code is in my Update function:

             if(Input.GetKeyDown(KeyCode.W) || Input.GetKeyDown(KeyCode.UpArrow)){
                 jumpButtonDown = true;
                 Debug.Log("Key Pressed "+jumpButtonDown);                
             }            
             if(jumpButtonDown==true){
                 moveDirection.y+=jumpSpeed;
             }                        
             if(Input.GetKeyUp(KeyCode.W) || Input.GetKeyUp(KeyCode.UpArrow)){
                 jumpButtonDown = false;
                 Debug.Log("Key Released "+jumpButtonDown);                
             }

So, what happens is that my debug log for the first if statement gets logged in the debugger and then the getKeyUp never triggers and the code in the 2nd if statement repeats on every update forever. However, if I remove the code from the 2nd if statement (and make it just empty), then the getKeyUp will trigger and I'll get the Debug Log message. Any idea why this isn't working? =\

Comment
Add comment · Show 6
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 Professor Snake · Jul 19, 2013 at 08:26 PM 0
Share

I'd rather work with Get$$anonymous$$ey ins$$anonymous$$d, it is probably much more reliable.

avatar image Jurassic · Jul 19, 2013 at 08:46 PM 0
Share

How would I get Get$$anonymous$$ey to work as I want it in this situation? I want to count how many frames a player is holding down a key and do something (push the player up on the Y axis) for each of the frames he's holding down the key.

avatar image perchik · Jul 19, 2013 at 08:51 PM 0
Share

It might have something to do with the OR on the input. $$anonymous$$aybe try to see if it works with just one of the keys?

avatar image Jurassic · Jul 19, 2013 at 08:55 PM 0
Share

Just tried removing the OR on both the keyup and the keydown, same result. :\

avatar image perchik · Jul 19, 2013 at 09:02 PM 0
Share

I just tested your code in my player and it works. I made the assumption that your boolean (jumpbuttondown) is declared globally, and it works fine:

 public class TestGUI : $$anonymous$$onoBehaviour {
 
     bool press;
     void Start () {
         press=false;
     }
     void Update () {
         if(Input.Get$$anonymous$$eyDown ($$anonymous$$eyCode.W)|| Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.UpArrow)){
             press=true;
             Debug.Log("$$anonymous$$ey pressed");
         }
         
         if(Input.Get$$anonymous$$eyUp($$anonymous$$eyCode.W) || Input.Get$$anonymous$$eyUp($$anonymous$$eyCode.UpArrow)){
             press= false;
             
             Debug.Log ("$$anonymous$$ey Released");
         }
             
     }
 }
Show more comments

2 Replies

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

Answer by Jurassic · Jul 19, 2013 at 09:32 PM

I figured it out! The problem was that my whole jump code was nested inside a giant isGrounded check. So the keyUp was first checking to see if it was grounded, and obviously he wouldn't be if he just jumped, so it would never fire. Lol. Thanks guys!

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
avatar image
0

Answer by Jamora · Jul 19, 2013 at 09:06 PM

I copy-pasted your code and it works perfectly for me.

You must either have your boolean defined inside Update (a local variable; reset every frame), or a null moveDirection, which would cause a null reference exception and break out of your update, never triggering your GetKeyUp.

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

18 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

Related Questions

Distribute terrain in zones 3 Answers

Why is my Input.GetKeyDown not working?? 2 Answers

Multiple Cars not working 1 Answer

error CS0117: `Input' does not contain a definition for `GetKeyDown' 1 Answer

Can Input.GetAxis be used without assigning via the Input Manager? 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