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 dan19 · Jul 27, 2013 at 09:52 AM · inputinput.getbutton

Exact meaning of Input.GetButtonDown()

Here's a paranoid question. What exactly do GetButtonDown(), GetButtonUp() and GetButton() return, and what is the relationship between them?

My own intuition tells me that GetButton() should return the state of the button at the instant when the frame began, GetButtonDown() should be equivalent to "``GetButton() && !GetButton_FromPreviousFrame``" and GetButtonUp() should be the same as "``!GetButton() && GetButton_FromPreviousFrame``". If so, then the following will always be true:

  1. GetButtonDown() always implies GetButton(), so GetButtonDown() && ~GetButton() will always equal false.

  2. GetButtonUp() always implies !GetButton(), so GetButtonUp() && GetButton() will always equal false.

  3. From 1 and 2 it follows that GetButtonDown() && GetButtonUp() will always equal false.

Are these assumptions correct? Is this properly documented anywhere? My worst fear is that if in the duration of one frame the user magically manages to press a button and then release it, then both GetButtonDown() and GetButtonUp() will return true, and the same will happen if, on the contrary, the button is first released and then quickly pressed. Then I won't be able to distinguish between these situations! And the fear is not purely hypothetical too: occasionally a long frame happens, that lasts for like 0.3 seconds, and it is physically possible to push a button and release it in that time.

UPDATE: ok, an update (apparently I cannot answer my own question). Assumptions 1 and 3 above are wrong, assumption 2 is in the gray area. The most important lesson is like this: GetButtonDown() and GetButtonUp() are not mutually exclusive and can both return true in the same frame.

Comment
Add comment · Show 10
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 dan19 · Jul 27, 2013 at 10:03 AM 0
Share

I've seen those pages, and they are not specific enough. They are not explicitly clear on what happens in the scenario that I'm talking about. They seem like introductory explanations, not addressing possible paranoid cases.

avatar image dan19 · Jul 27, 2013 at 10:07 AM 0
Share

For instance, description of GetButton() says "Returns true while the virtual button identified by buttonName is held down.". That cannot be true to the letter, because GetButton() will always return the same value during one frame, and the actual state of the button can change, and even more than once.

avatar image dan19 · Jul 27, 2013 at 10:53 AM 0
Share

Of course like this only one log message will appear. You use if-else-if.

avatar image dan19 · Jul 27, 2013 at 11:05 AM 0
Share

O$$anonymous$$, I've made an actual experiment, and managed to get GetButtonDown() and GetButtonUp() to return true in the same frame, although I had to make frames long by loading Update() with useless work.

avatar image dan19 · Jul 27, 2013 at 11:10 AM 0
Share

I'll add the code to my answer now.

Show more comments

1 Reply

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

Answer by Benproductions1 · Jul 29, 2013 at 02:56 AM

Hello,

Think of it this way: The only "real" input that the Unity application gets, is gathered into a queue from the operating system. This only includes key based up and down events. This queue is then stored and evaluated using "Input" before Update is called

KeyUp - There was an up event for this key between this frame and the last
KeyDown - There was a down event for this key in between this frame and the last
Key - There has previously been a KeyDown event for this key, but no KeyUp yet

The same goes for button (obviously through the input manager). This both explains why key up/down don't work in the fixed update loop, but just key does. It also explains how you can have both a KeyUp and KeyDown event for the same key on a frame.

If you want a coding example for how this is implemented in the backend, I can post that too :)
Hope this helps,
Benproductions1

Comment
Add comment · Show 2 · 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 dan19 · Jul 29, 2013 at 06:16 AM 0
Share

Thanks a lot. This explains my experimental data perfectly. Of course, a code example would be great!

avatar image oxysofts · Jan 08, 2017 at 10:03 AM 0
Share

so how is that acceptable? This is just an awful design. Up should be true for the frame that it was released and Down should be true for the frame that it was pressed down. No question about it. This is an engine for writing games for fuck sake, you would expect everything to be logical and definite, not this crap.

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

19 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

Related Questions

Input not working at all! 1 Answer

Unable to check input action 0 Answers

Is there a way to do different things depending on whether the button is being held or tapped? 2 Answers

Help In Making a SphereCast for 3D Tire! Working RayCast Script included! 0 Answers

Using input.getbuttondown to play audio = NOT WORKING? :( 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