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
2
Question by Hamesh81 · Oct 11, 2013 at 07:24 AM · inputtimemultiplegetbuttondowngetbutton

How can I use multiple GetButtonDown keys simultaneously?

I have a strange dilemma. I am working on a type of "jump" functionality where I want the jump distance/force to be the same every time the player presses the input, I DON'T want the jump distance/force to increase as the player holds the button. GetButtonDown seems the ideal solution for this, but the problem is that I am using 2 keys (need to be pressed simultaneously) to trigger the jump, and naturally having to press 2 keys within the same frame is very bad for player usability reasons.

So, I need the input window to be long enough to make it "usable" to press 2 keys at once, but at the same time trigger the jump force only once. I've tried using a combination of GetButton & GetButtonDown to trigger the jump but the results have been inconsistent. Any suggestions for 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
6
Best Answer

Answer by syclamoth · Oct 11, 2013 at 07:37 AM

Try doing something like this:

 if((GetButtonDown("KeyOne") && GetButton("KeyTwo"))
  || (GetButtonDown("KeyTwo") && GetButton("KeyOne"))
 {
     // Do jump!
 }

This allows the action to be performed when you are already holding one of the buttons, and you press down the second one.

For additional tweaking, add a time factor as well:

 //Make sure you have a float 'buttonPressedTime'
 // Also, this has to happen *after* the other check, or it will invalidate the time!
 if(GetButtonDown("KeyOne") || GetButtonDown("KeyTwo")) {
     buttonPressedTime = Time.time;
 }

Then, make sure that Time.time is within some short window of 'buttonPressedTime' when the player does the two-key action to ensure that the buttons were pressed close together.

Comment
Add comment · Show 6 · 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 Hamesh81 · Oct 11, 2013 at 07:41 AM 0
Share

Hmmm, this seems it could work for me. So theoretically the jump force will still be applied in one frame only since "GetButtonDown && GetButton" will only be true for one frame is that right?

avatar image syclamoth · Oct 11, 2013 at 07:44 AM 0
Share

Yes, because triggering the action requires at least one 'GetButtonDown' to return true, and GetButtonDown only activates once per key-press.

avatar image Hamesh81 · Oct 11, 2013 at 08:21 AM 0
Share

Great! I'll test this out when I get the chance. Thanks a lot

avatar image Hamesh81 · Oct 11, 2013 at 08:59 AM 0
Share

I had to add a timer, since in 1 frame a character doesn't jump anywhere. So to get it working the two keys need to trigger the timer first during which the force can be applied, if that makes sense. Thanks for your help

avatar image Hamesh81 · Oct 18, 2013 at 01:21 PM 1
Share

Done and done!

Show more comments
avatar image
1

Answer by Hamesh81 · Oct 11, 2013 at 04:31 PM

Just want to post here the solution I actually ended up using for anyone else with a similar problem. After further testing I found the above solution still inconsistent at times with the occasional misfire of the button inputs. The solution is a very simple one:

 if (GetButton("KeyOne") && GetButton("KeyTwo") {
     doJump = true;
 }

 if (doJump) {
     //Do Jump, addforce etc...
     //Then turn bool off
     doJump = false;
 }

I simply used GetButton for both keys which means there is no chance of misfiring like there is with GetButtonDown. Once both keys are being held a boolean is set to true. This is the key part. The boolean is listening for the first instance when both keys are pressed, and because it is a boolean, no matter how long/short you hold both keys for it will always apply the same amount of force to the jump because it is only applying it once. Hence it works like a GetButtonDown, but for multiple keys at once ;)

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

Detecting if 2 Buttons are pressed at the same time 3 Answers

Having GetButton act like GetKeyDown 1 Answer

Simultaneous button presses 2 Answers

C# Problem returning from crouch to stand 1 Answer

Modifying properties of a buttonName 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