Controller input
Author(s): catinsuranceTags:
Game controllers can complicate detecting arbitrary button presses.
Controller detection⚓︎
Controllers can easily be detected by reading the ControllerIndex
property of the player. If it is above 0
, then the player must be using a controller, as 0
is always the ControllerIndex
of the keyboard.
1 2 |
|
Controllers and Input.IsButtonTriggered/Pressed()
⚓︎
Controllers can be detected by Input.IsButtonTriggered()
and Input.IsButtonPressed()
, but not without some caveats.
Firstly, controller inputs don't have a value in the Keyboard
enum, but they do have a number which corresponds to what button you're pressing.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
|
Secondly, controllers trigger multiple values at once. Pressing a button on the controller will trigger 4 different values in the Keyboard
enumerator. This can cause complications for detecting keyboard input, because a controller can trigger a value that you're not expecting.
Reproduction code
1 2 3 4 5 6 7 8 9 |
|
To detect if a keyboard button was triggered by a controller, you can modulo the input by 32. This will always return the correct value of the button pressed.
1 2 3 |
|