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 adnanzmn97 · Sep 10, 2020 at 04:50 AM · inputplayerdirectionactionkeys

How to detect two keys released simultaneously [Input System]

I want the player to turn to face the direction of input, and stay facing that direction even after the keys are released. The problem is that when two buttons are pressed and the player's facing a diagonal, letting go of both keys leaves the player facing in one of two directions (either forward/backward or left/right) but never diagonal.

Looking at the console it seems that when I release both keys, for a few frame it's registering one of the axes being pressed and not the other. No matter how exact I try to be, there's at least one frame where one keys pressed and not the other.

How would I be able to detect that both keys were released?

 void Update()
     {
         movementDir = movement.ReadValue<Vector2>();
         
         //if movementDir != (0,0)
         if (movementDir.magnitude > 0.01f)
         {
             transform.forward = new Vector3(movementDir.x, 0, movementDir.y);
             Debug.Log(movementDir.magnitude + " " + movementDir);
         }
         else
         {
             Debug.Log("pressing nothing: " + movementDir);
         }            
     }

alt text

releasetwokeys.png (25.3 kB)
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

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by rh_galaxy · Sep 11, 2020 at 04:21 AM

You could make a delay for movement, so it must be the same for example for 4 frames to give a new movement, like this:

 Vector2[] moveLastDir = new Vector2[4];
 Vector2 moveNewDir = new Vector2(0, 0);
 int curDir = 0;
 
 moveLastDir[curDir] = movement.ReadValue<Vector2>();
 if (moveLastDir[(curDir + 3) % 4] == moveLastDir[curDir]) moveNewDir = moveLastDir[curDir];
 curDir++; curDir %= 4;
 
 if (moveNewDir.magnitude > 0.01f) {
     transform.forward = new Vector3(moveNewDir.x, 0, moveNewDir.y);
 }
 Debug.Log(moveNewDir.magnitude + " " + moveNewDir);

But if you have a fast framerate like 1000 you will still have problems with buttons not releasing at the same frame. So the best thing would be to implement some steering towards a direction. And either take the nearest direction of the 8 distinct 45 deg directions or have free steering angle.

Comment
Add comment · Show 3 · 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 adnanzmn97 · Sep 11, 2020 at 05:06 PM 0
Share

Thank you that method works! I can see how at higher framerates there would still be problems. By steering do you mean that each frame the character rotates a bit towards the target direction instead of being set to exactly to the target direction?

avatar image rh_galaxy adnanzmn97 · Sep 11, 2020 at 05:29 PM 1
Share

Yes, so that you change the direction a little each frame towards a goal, and either have that as the direction or round it to the nearest 45 deg angle. But it depends on what type of game/program you are making.

avatar image adnanzmn97 rh_galaxy · Sep 11, 2020 at 06:06 PM 0
Share

Got it, that makes sense. Thanks for your help!

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

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

Related Questions

How to consume input when automatically switching control schemes through a Player Input Component 0 Answers

transforming player position on input.get button down 1 Answer

How to use PlayerInput In Conjunction with C# Events? 0 Answers

Have a character face their moving direction? 1 Answer

Trying to find what player inputed in Text Input field 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