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
2
Question by MilesAway1980 · Dec 15, 2015 at 12:55 PM · unity 5inputcontrollerkeyboard

How to clear the keyboard buffer

Hello all,

I'm having an issue with the game that I am making. I currently have the controls set up to use both a keyboard and a gamepad.

On the joystick the shoulder buttons turn left and right, and on the keyboard, either A/D or the left/right arrow keys turn left and right.

If I hold down the shoulder buttons on the gamepad, the character will turn until the very moment that I release the button, which is great.

However, the problem I'm running into is that the longer I hold down a key on the keyboard, the longer the character will continue to still turn after I have released the key. I know when working on games in the past that this problem stemmed from the keyboard storing up keypresses in the buffer, and then continuing to register them as held until it emptied. Clearing the buffer after every read resolved the issue.

Is there a way to do that in Unity?

Here's what's in Project Settings -> Input :

 Horizontal:
   Name:  Horizontal
   Negative Button: left
   Positive Button: right
   Alt Negative Button: a
   Alt Positive Button: d
   Gravity: 1
   Dead:  0.001
   Sensitivity:  1
   Type:  Key or Mouse Button
   Axis:  X axis
 
 Vertical:
   Name:  Vertical
   Negative Button: down
   Positive Button: up
   Alt Negative Button: w
   Alt Positive Button: s
   Gravity: 1
   Dead:  0.001
   Sensitivity:  1
   Type:  Key or Mouse Button
   Axis:  Y axis

Here's the code for my controls:

 float keyboardHorizontal = Input.GetAxis ("Horizontal");
 float keyboardVertical = Input.GetAxis ("Vertical");
     
 if (buttons [6].getHeld () || keyboardHorizontal < 0) {
   if (!current.turningLeft) {
     current.turningLeft = true;
     CmdTurnShip (1);
   }
 } else {
   if (current.turningLeft) {
     current.turningLeft = false;
     CmdTurnShip (0);
   }
 }
     
 if (buttons [7].getHeld () || keyboardHorizontal > 0) {
   if (!current.turningRight) {
     current.turningRight = true;
     CmdTurnShip (-1);
   }
 } else {
   if (current.turningRight) {
     current.turningRight = false;
     CmdTurnShip (0);
   }
 }

 [Command]
 void CmdTurnShip (int dir) {
   ship.setTurn(dir);
 }


 class Ship() {
     float angle;
     public void setTurn(int dir) {
         angle += dir;
         transform.Rotate (new Vector3 (0, 0, dir));
     }
 }


The way it is designed to work is for the ship to receive the signal to turn when the button is pressed. It will then continue to turn every Update() for as long as the boolean for it to turn is true (which is set in the CmdTurnShip() function). Then, when the axis is released, the code tells it to stop turning.

This works perfectly with the Joystick, but the keyboard will lag after it is released. The longer it is held, the longer it will go before the input changes. There is nothing in my code that stores presses. If I simply print the current state of Input.GetAxis("Horizontal") to the console, it, too, will show that it is pressed down after I release the key.

Thanks

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
0

Answer by Dave-Carlile · Dec 15, 2015 at 12:59 PM

If you're trying to turn continuously while a key is being held down you should be using the GetKey/GetButton functions, not the GetKeyDown/GetButtonDown functions. The keyboard buffer shouldn't be involved at all.

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 MilesAway1980 · Dec 17, 2015 at 02:47 AM 1
Share

Thanks for the reply. I am actually using the GetAxis command (not the Get$$anonymous$$eyDown).

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

keyboard input (num pad) when unity does not have focus 0 Answers

I have an issue with the code for Submenu. Someone to help me ? 1 Answer

Contollers are unusable when already plugged in when starting build/editor 0 Answers

How to disable keyboard input on modal window? 1 Answer

How to create a menu with 2 keyboard cursors? 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