Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 David009 · Dec 08, 2016 at 06:45 AM · c#2dinputbuttons

How to execute action with two keys when they are alredy tied to other actions?

Hi, I'm a beginner programmer. I'm making a player rotation input method and I'm having trouble with making a 45 degree rotation by pressing two keys (up and right for example). The trouble is that the gameplay (2d top down game) is sort of timed dexterity thing and it requires only one attempt at rotation, so if the player wants to rotate to 45 and presses up and right at once the program registers either up or right rotation first and only then the 45 degree middle position. I think that I need some kind of very short timer during which the program will check is some other key pressed instantly after and make the rotation and if it is not then it will only rotate according to the first key. I tried several stuff but i can't make it work. Thanks in advance for any help.

Here's an example of my code for rotation without the middle 45 degree position.

 public void HandleInput()
 {          
     if (Input.GetKeyDown(KeyCode.UpArrow))
     {
         transform.rotation = Quaternion.Euler(new Vector3(0, 0, 90));
     }     
     if (Input.GetKeyDown(KeyCode.RightArrow))
     {
         transform.rotation = Quaternion.Euler(new Vector3(0, 0, 0));
     }
 }
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 David009 · Dec 12, 2016 at 10:18 PM

Just to post a solution to this. This gets called when a player presses first key.

 IEnumerator CheckForRightButton()
 {
 int counter = 0;
 bool rotated = false;

 //here i check for five frames did the player press another key after the first one
 while (counter < 5)
 {
     if (Input.GetKeyDown(KeyCode.RightArrow))
     {
         transform.rotation = Quaternion.Euler(new Vector3(0, 0, 45));
         rotated = true;
         Debug.Log("Up/Right");
         yield break;
     }

     counter++;
     yield return null;
 }
    //if just one key was pressed then this is executed
    if (!rotated)
    {
        transform.rotation = Quaternion.Euler(new Vector3(0, 0, 90));
        Debug.Log("Up");
    }
 }
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 UnityCoach · Dec 09, 2016 at 11:38 AM

You want to exclude each others from the first conditions, like :

  public void HandleInput()
  {          
      if (Input.GetKeyDown(KeyCode.UpArrow) && !Input.GetKeyDown(KeyCode.RightArrow))
      {
          transform.rotation = Quaternion.Euler(new Vector3(0, 0, 90));
      }     
      else if (Input.GetKeyDown(KeyCode.RightArrow) && !Input.GetKeyDown(KeyCode.UpArrow))
      {
          transform.rotation = Quaternion.Euler(new Vector3(0, 0, 0));
      }
      else if (Input.GetKeyDown(KeyCode.RightArrow) && Input.GetKeyDown(KeyCode.UpArrow))
      {
          // third action
      }
  }

or use a mutual exclusive state (finite state) using an enum

      public enum STATE {None, Up, Right, UpRight};
      private STATE _state;
 
      public void HandleInput()
      {          
          if (Input.GetKeyDown(KeyCode.UpArrow) && !Input.GetKeyDown(KeyCode.RightArrow))
          {
              _state = STATE.Up;
          }     
          if (Input.GetKeyDown(KeyCode.RightArrow) && !Input.GetKeyDown(KeyCode.UpArrow))
          {
              _state = STATE.Right;
          }
          if (Input.GetKeyDown(KeyCode.RightArrow) && Input.GetKeyDown(KeyCode.UpArrow))
          {
              _state = STATE.UpRight;
          }
 
          switch (_state)
          {
              case STATE.Up :
              transform.rotation = Quaternion.Euler (Vector3.Up);
              break;
              case STATE.Right :
              transform.rotation = Quaternion.Euler (Vector3.Right);
              break;
              case STATE.UpRight :
              transform.rotation = Quaternion.Euler (new Vector3 (0, 0, 45));
              break;
          }
      }



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 David009 · Dec 10, 2016 at 01:27 PM 0
Share

The problem is that Get$$anonymous$$eyDown lasts only for one frame, both keys are never going to be registered at the same frame, because it's nearly impossible to press them that evenly.

I ended up using a coroutine to check for another button, during five frames after the first one is pressed.

       IEnumerator CheckForRightButton()
 {
 int counter = 0;
 bool rotated = false;

 //here i check for five frames did the player press another key after the first one
 while (counter < 5)
 {
     if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.RightArrow))
     {
         transform.rotation = Quaternion.Euler(new Vector3(0, 0, 45));
         rotated = true;
         Debug.Log("Up/Right");
         yield break;
     }

     counter++;
     yield return null;
 }
    //if just one key was pressed then this is executed
    if (!rotated)
    {
        transform.rotation = Quaternion.Euler(new Vector3(0, 0, 90));
        Debug.Log("Up");
    }
 }

avatar image UnityCoach David009 · Dec 10, 2016 at 08:03 PM 0
Share

You're right. You may also want to use Get$$anonymous$$ey() ins$$anonymous$$d of Get$$anonymous$$eyDown(). Get$$anonymous$$ey() is true every frame the key is pressed.

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

8 People are following this question.

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

Related Questions

Input.GetButton not working 1 Answer

NullRefrenceException with new input system 0 Answers

Distribute terrain in zones 3 Answers

Divide screen in half for two buttons 1 Answer

How to keep my cursor inside window boundaries 0 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