Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
This question was closed Sep 13, 2015 at 10:54 PM by Burton-kun for the following reason:

La domanda ha avuto risposta, è stata accettata la risposta giusta

avatar image
0
Question by Burton-kun · Aug 26, 2015 at 07:49 PM · c#input.getkeyaccuracy

Double input.GetKey conditions accuracy

My apologies if this is been already asked but I searched and I found nothing. I'm using these conditions to manage player's facing direction:

         if (Input.GetKey (KeyCode.UpArrow)) {
             transform.forward = new Vector3 (0, 0, -1);
             direction = "N";
         } else if (Input.GetKey (KeyCode.DownArrow)) {
             transform.forward = new Vector3 (0, 0, 1);
             direction = "S";
         } else if (Input.GetKey (KeyCode.LeftArrow)) {
             transform.forward = new Vector3 (1, 0, 0);
             direction = "W";
         } else if (Input.GetKey (KeyCode.RightArrow)) {
             transform.forward = new Vector3 (-1, 0, 0);
             direction = "E";
         }
         if (Input.GetKey (KeyCode.UpArrow) && Input.GetKey (KeyCode.RightArrow)) {
             transform.forward = new Vector3 (-1, 0, -1);
             direction = "NE";
         } else if (Input.GetKey (KeyCode.RightArrow) && Input.GetKey (KeyCode.DownArrow)) {
             transform.forward = new Vector3 (-1, 0, 1);
             direction = "SE";
         } else if (Input.GetKey (KeyCode.DownArrow) && Input.GetKey (KeyCode.LeftArrow)) {
             transform.forward = new Vector3 (1, 0, 1);
             direction = "SW";
         } else if (Input.GetKey (KeyCode.LeftArrow) && Input.GetKey (KeyCode.UpArrow)) {
             transform.forward = new Vector3 (1, 0, -1);
             direction = "NW";
         }

I'm also storing the current direction in a variable. The problem is that there's a lack of accuracy if we talk about a double Input.GetKey condition, because the last of the two keys that gets left, gives me back the wrong value of the variable, even if the player is facing the currect diagonal direction (not always, often happens that the player doesn't stop in diagonal direction). I just want the two keys to give me back the right value and the right direction facing even if left at different times. How can I fix this problem?

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

1 Reply

  • Sort: 
avatar image
0
Best Answer

Answer by kromenak · Aug 26, 2015 at 08:18 PM

Input.GetKey will constantly return either true or false, depending on whether the key is currently pressed or not. On the other hand, Input.GetKeyDown will only return true on the frame the key is initially pushed down, and Input.GetKeyUp will only return true on the frame the key is released.

It seems that what you really want here is to only check for a direction change when a key is pressed down, not when a key is released. As a result, you could maybe try something like this:

 private void Update()        
 {
         // Only refresh the direction when a key is initially pressed down.
         if(Input.GetKeyDown(KeyCode.UpArrow)
            || Input.GetKeyDown(KeyCode.DownArrow)
            || Input.GetKeyDown(KeyCode.LeftArrow)
            || Input.GetKeyDown(KeyCode.RightArrow))
         {
                 RefreshDirection();
         }
 }
 
 private void RefreshDirection()
 {
     if (Input.GetKey (KeyCode.UpArrow)) {
              transform.forward = new Vector3 (0, 0, -1);
              direction = "N";
          } else if (Input.GetKey (KeyCode.DownArrow)) {
              transform.forward = new Vector3 (0, 0, 1);
              direction = "S";
          } else if (Input.GetKey (KeyCode.LeftArrow)) {
              transform.forward = new Vector3 (1, 0, 0);
              direction = "W";
          } else if (Input.GetKey (KeyCode.RightArrow)) {
              transform.forward = new Vector3 (-1, 0, 0);
              direction = "E";
          }
          if (Input.GetKey (KeyCode.UpArrow) && Input.GetKey (KeyCode.RightArrow)) {
              transform.forward = new Vector3 (-1, 0, -1);
              direction = "NE";
          } else if (Input.GetKey (KeyCode.RightArrow) && Input.GetKey (KeyCode.DownArrow)) {
              transform.forward = new Vector3 (-1, 0, 1);
              direction = "SE";
          } else if (Input.GetKey (KeyCode.DownArrow) && Input.GetKey (KeyCode.LeftArrow)) {
              transform.forward = new Vector3 (1, 0, 1);
              direction = "SW";
          } else if (Input.GetKey (KeyCode.LeftArrow) && Input.GetKey (KeyCode.UpArrow)) {
              transform.forward = new Vector3 (1, 0, -1);
              direction = "NW";
          }
 }

By doing this, you check for a direction change when a key is pressed down, but if the player releases the down key while still holding the left key, it won't change the direction.

But there are some other things you may need to take into account. For example, what if a player releases the down key, and holds the left key, but the player's intention IS to change the direction to West? As a result, when a key is released, you may want a short delay, after which the direction actually will refresh, because it becomes clear that the player intention is to change the direction.

Comment
Add comment · Show 1 · 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 Burton-kun · Aug 26, 2015 at 09:04 PM 0
Share

Now I get the opposite problem ins$$anonymous$$d. By doing this way, when I walk, if I leave one of the two arrows to move on a vertical or horizontal axis ins$$anonymous$$d of the diagonal axis, the player keeps facing the diagonal directions while walking vertical/horizontal.

EDIT: Sorry but I didn't see the last part of the message. I tried to set a delay. It seems to work. The direction is the right one in any case. I write here the code to know if it's really ok and in this case to help if someone will have my same question.

 private void Update () {
 if (Input.Get$$anonymous$$eyDown ($$anonymous$$eyCode.UpArrow)
             || Input.Get$$anonymous$$eyDown ($$anonymous$$eyCode.DownArrow)
             || Input.Get$$anonymous$$eyDown ($$anonymous$$eyCode.LeftArrow)
             || Input.Get$$anonymous$$eyDown ($$anonymous$$eyCode.RightArrow)) {
             RefreshDirection ();
         } else {
             Delay++;
         } 
         
         if (Delay > 7.25f) {
             RefreshDirection ();
         }
 }
 
 public void RefreshDirection(){
         Delay = 0f;
             if (Input.Get$$anonymous$$ey ($$anonymous$$eyCode.UpArrow)) {
                 transform.forward = new Vector3 (0, 0, -1);
                 direction = "N";
             } else if (Input.Get$$anonymous$$ey ($$anonymous$$eyCode.DownArrow)) {
                 transform.forward = new Vector3 (0, 0, 1);
                 direction = "S";
             } else if (Input.Get$$anonymous$$ey ($$anonymous$$eyCode.LeftArrow)) {
                 transform.forward = new Vector3 (1, 0, 0);
                 direction = "W";
             } else if (Input.Get$$anonymous$$ey ($$anonymous$$eyCode.RightArrow)) {
                 transform.forward = new Vector3 (-1, 0, 0);
                 direction = "E";
             }
 
         if (Input.Get$$anonymous$$ey ($$anonymous$$eyCode.UpArrow) && Input.Get$$anonymous$$ey ($$anonymous$$eyCode.RightArrow)) {
             transform.forward = new Vector3 (-1, 0, -1);
             direction = "NE";
         } else if (Input.Get$$anonymous$$ey ($$anonymous$$eyCode.RightArrow) && Input.Get$$anonymous$$ey ($$anonymous$$eyCode.DownArrow)) {
             transform.forward = new Vector3 (-1, 0, 1);
             direction = "SE";
         } else if (Input.Get$$anonymous$$ey ($$anonymous$$eyCode.DownArrow) && Input.Get$$anonymous$$ey ($$anonymous$$eyCode.LeftArrow)) {
             transform.forward = new Vector3 (1, 0, 1);
             direction = "SW";
         } else if (Input.Get$$anonymous$$ey ($$anonymous$$eyCode.LeftArrow) && Input.Get$$anonymous$$ey ($$anonymous$$eyCode.UpArrow)) {
             transform.forward = new Vector3 (1, 0, -1);
             direction = "NW";
         }
     }

Follow this Question

Answers Answers and Comments

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

Related Questions

I'd like my object to be active only when space key is NOT pressed 1 Answer

c# - Make something happen if I press a sequence of keys 4 Answers

Input.GetKey("Horizontal")... why its not working? 1 Answer

Set new default position each time? 1 Answer

Using one key only, change bool to true or false depending on it's current value 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