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 /
avatar image
0
Question by Larry-Dietz · Dec 05, 2017 at 06:59 AM · input.getkey

Strange occurrence with input

The following is being called from Update. What I expected to happen is that when a key press occurs, QueuedMove is set to the appropriate direction.

Once the move timer hits 0, the move occurs.

Then the QueuedMove is reset to Still so another move doesn't occur.

If they continue to hold the key, I expect it to set the QueuedMove again each update. But if the key is not being held, only 1 move should occur.

What is actually happening, is when I press the key, no matter how briefly, the move occurs twice.

I added a couple of debug statements, on moving left, to see what was going on, and the log shows the Queued Left, then Moving, then Queued Left again, then moving again.

Can anyone see what I might be doing wrong here, that would cause this to move twice?

I have been staring at this for hours, and can't seem to figure out where I am going wrong.

  public void ProcessKeyboardInput()
     {
         if (Input.GetKey(KeyCode.LeftArrow))
         {
             Debug.Log("Queued Left");
             QueuedMove = MovingDirection.Left;
         }
         if (Input.GetKey(KeyCode.RightArrow))
             QueuedMove = MovingDirection.Right;
         if (Input.GetKey(KeyCode.DownArrow))
             QueuedMove = MovingDirection.Down;
         if (Input.GetKey(KeyCode.UpArrow))
             QueuedMove = MovingDirection.Up;
       
         MoveTime -= Time.deltaTime;
         if (MoveTime < 0)
         {
             if (QueuedMove != MovingDirection.Still)
             {
                 Debug.Log("Moving");
                 MovePlayer(QueuedMove);               
                 QueuedMove = MovingDirection.Still;
 
                 MoveTime = MoveDelay;
             }
         }
     }

Any help is greatly appreciated! -Larry

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 Dragate · Dec 05, 2017 at 07:38 AM

Something is fishy with that MoveTime -= Time.deltaTime; I believe it's a problem that it's being decremented even when MovingDirection.Still. Could you try

 if (QueuedMove != MovingDirection.Still){
   if(MoveTime < 0){
      Debug.Log("Moving");
      MovePlayer(QueuedMove);               
      QueuedMove = MovingDirection.Still;
      MoveTime = MoveDelay;
   }else{
      MoveTime -= Time.deltaTime;
   }
 }

EDIT: Explanation is that when at MovingDirection.Still, the timer was counting. When you pressed an arrow (which probably is pressed for more than one frame, even for fast presses), you instantly moved at that frame (since timer had counted at .Still and was already negative) and on next frame it buffered another movement. So, that's 2 movements with 1 press.

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 Larry-Dietz · Dec 05, 2017 at 07:54 AM 0
Share

You got it!!! Not sure why I wasn't spotting this as being a problem. Didn't expect that being decremented every frame to cause this to occur, but it did.

I appreciate your help on this! -Larry

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

117 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

Related Questions

Why pressing the up and down key doesn't mean the body in correct forward and back direction? 1 Answer

Unity input not working "up arrow",KeyBoard Up arrow input not working 0 Answers

Why doesn't Input.GetButtonDown() work with my ps4 controller? 0 Answers

input.getkey keycode scrollwheel - Is this even possible? 0 Answers

How can I get inputs in several steps? 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