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 lloydlll · Jun 19, 2015 at 07:57 AM · inputbooleanmultiplegetbuttondowngetbutton

Detecting if 2 Buttons are pressed at the same time

I am trying to make it so that you are not able to press more then one button at a time. If you do I want to put be able to detect it and have a bool saying multiple buttons are pressed. I have it set up so that if more then one button is pushed the character doesn't move. That is done by

 if (input.getkeydown(Keycode.uparrow) && !input.getkeydown(Keycode.Rightarrow)){
  //movement code
 }

That is just a portion of the code, showing how I did it, I have more buttons and all but one has a !. I would like to have a simple bool that is can just put into an if statement if possible.

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 KayelGee · Jun 19, 2015 at 10:48 AM 0
Share

What are you trying to do by knowing if multiple buttons are pressed? Do you want to tell the player "you're pushing multiple buttons, that's not ok" or do you simply want your input to process only one input?

avatar image lloydlll · Jun 19, 2015 at 02:59 PM 0
Share

The reason is that I want to make it harder to move, there is no diagonal movement in the game so when two buttons are pressed I want to make a red circle with a line through it appear and there will be no movement. Basically the idea is in the heat of the moment you panic and try to press more then one button you won't move and an enemy will get you. How would you reset the number count after buttons are released?

3 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Jarnak · Jun 19, 2015 at 02:53 PM

Well, you could put this in your Update(). There is surely a better way, but it's the first that came into my mind.

 int count = 0;
 bool tooManyKey = false;
 foreach (KeyCode kc in System.Enum.GetValues(typeof(KeyCode))
 {
   if (Input.GetKeyDown(kc))
     count++;
   if (count>1)
   {
     tooManyKey = true;
     break;
   }
 }

I don't know why do you need that, but maybe you should better use GetKey instead of GetKeyDown. If you use GetKeyDown, the bool will turn true only if you simultaneously press 2 buttons, not if you hold one and then press another. It's your choice to make x)

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 lloydlll · Jun 19, 2015 at 03:28 PM 0
Share

This does not reset the count back to 0 when no buttons are pressed so too$$anonymous$$any$$anonymous$$ey will always be active after 2 keys are pressed.

avatar image Jarnak · Jun 22, 2015 at 08:20 AM 0
Share

I wrote "you could put this in your Update()", so yes too$$anonymous$$any$$anonymous$$ey will always be active during the frame it was activated. Not so long so.

avatar image
0

Answer by Baste · Jun 19, 2015 at 11:02 AM

A big question here is "why?". You usually want to allow the player to press several buttons at once. Also, GetButtonDown only registers on the frame the button is pressed - which means that two of them being true only happens if the player manages to press them approximately at the same time. That being said, this is not very difficult to code up:

 bool rightPressed = Input.GetKey(KeyCode.RightArrow);
 bool leftPressed = Input.GetKey(KeyCode.LeftArrow); 
 bool upPressed = Input.GetKey(KeyCode.UpArrow);
 bool downPressed = Input.GetKey(KeyCode.DownArrow);

 int numPressed = 0;
 if (rightPressed)
     numPressed++;
 if (leftPressed)
     numPressed++;
 if (upPressed)
     numPressed++;
 if (downPressed)
     numPressed++;

 if (numPressed == 1) {
     if (upPressed) {
         MoveUp();
     }
     ...
 }
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 LaneFox · Jun 19, 2015 at 04:07 PM

Assuming the keys are known, I would use a static bool to gate the input logic.

     public class Keys : MonoBehaviour
     {
         public static bool SomeKeyIsDown;
 
         void Update()
         {
             if (!SomeKeyIsDown && Input.GetKeyDown(KeyCode.LeftArrow))
             {
                 SomeKeyIsDown = true;
                 transform.position = (transform.position + Vector3.left*Time.deltaTime);
             }
 
             if (!SomeKeyIsDown && Input.GetKeyDown(KeyCode.RightArrow))
             {
                 SomeKeyIsDown = true;
                 transform.position = (transform.position + Vector3.right*Time.deltaTime);
             }
 
             // etc...
 
             else SomeKeyIsDown = false;
 
             }
         }
     }

You could also make a static method to control the bool if you didn't want to toggle it in each input condition.

 public class KeyCheck : MonoBehaviour
 {
     public static bool SomeKeyIsDown;
 
     void Update()
     {
         SomeKeyIsDown = 
             Input.GetKeyDown(KeyCode.RightArrow) | 
             Input.GetKeyDown(KeyCode.LeftArrow) |
             Input.GetKeyDown(KeyCode.UpArrow) |
             Input.GetKeyDown(KeyCode.DownArrow);
         // etc...
     }
 }


I guess there are problems with doing this exactly as is, but you get the idea.

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

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How can I use multiple GetButtonDown keys simultaneously? 2 Answers

Having GetButton act like GetKeyDown 1 Answer

Simultaneous button presses 2 Answers

Having trouble checking if the player is in a trigger. 2 Answers

Having issues with Input.GetKey... 3 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