- Home /
How to Determine Player Input Method
Is there a way to determine the input method the player is using with the new Unity Input System?
For example, I would like to write a function with if statements where the flow is determine on whether the player is using the mouse, a controller, etc.
Answer by finnjwohner · Jul 12, 2020 at 01:46 AM
You can check if the user is using a gamepad, keyboard, and or mouse, the following will either result in null or a variable of it's type:
Using UnityEngine.InputSystem;
private void Start() {
Gamepad gamepad = Gamepad.current;
Keyboard keyboard = Keyboard.current;
Mouse mouse = Mouse.current;
if (gamepad) {
// Player is using a gamepad
}
if (keyboard) {
// Player is using a keyboard
}
if (mouse) {
// Player is using a mouse
}
}
Hope that can help :)
I was almost there! I had the references setup but i didn't know it was as simple as using the variable they are tied to!
Thanks a ton!
Doesn't work for me. "Cannot implicitly convert type 'UnityEngine.InputSystem.Gamepad' to 'bool' I'm not really sure why the if statements as written above would ever work. Changing the inside of the if statement to if(gamepad != null) will at least compile, but doesn't really have the desired effect.