Home > OS >  How to check for both WASD and arrow keys input simultaneously in unity?
How to check for both WASD and arrow keys input simultaneously in unity?

Time:08-03

I am working on a game that has similar player mechanics as enter image description here

But....

Input.GetAxis(string) and Input.GetAxisRaw(string) works similar to Input.GetKey(KeyCode) but I want something that works as Input.GetKeyDown(KeyCode).


PS: I would appreciate an answer that doesn't include getting away with boolean flags to skip frames after the button is pressed down.

CodePudding user response:

The Unity InputManager allows you to specify a primary key and alternative keys for the positive and negative buttons on an axis.

You can configure your "horizontal" axis to use "w" for positive, "s" for negative, "up arrow" for alt positive, and "down arrow" for alt negative. Then you should use GetAxisRaw.

Using the InputManager allows you to abstract the configuration of inputs out of your code. The InputManager can be configured to use a controller, a racing setup, a keyboard, etc, all without having to make any modifications to your code. While it functions identically to GetKey, it allows you to avoid hardcoding button presses.

A side note for why you use GetAxisRaw: GetAxis smooths input, which is why it takes some time for it to hit 1 or -1. GetAxisRaw delivers the immediate input.

It appears that you do not actually want WASD/arrow continuous control of your character and instead want impulse control. In other words, you don't want longer button presses to further affect your character's movement. Instead, consider using GetButtonDown and define the buttons appropriately in your InputManager.

  • Related