- Home /
How do I detect any input on a gamepad?
Hi guys,
I want my game to be playable on keyboard and on gamepad, and I want to offer an immediate and invisible transition from one to the other. I'm using the (now not so) new input system and it worked well until I need to slightly differentiate a behavior depending on the inputs coming from the keyboard or the gamepad (something about the camera)
For this I was planning to have a boolean to keep track of the origin of the latest input.
if (Keyboard.current.anyKey.wasPressedThisFrame)
{
keyboardIsTheActiveController = true;
}
else if (Gamepad.current.anyKey.wasPressedThisFrame)
{
keyboardIsTheActiveController = false; // it's th gamepad.
}
The problem is that Gamepad.current.anyKey doesn't exist, nor anyControl, anyInput, or whatever.
How do I detect any input on a gamepad?
Answer by LoloBad · Feb 28, 2021 at 03:30 PM
OK, I found a solution from this very interesting one-year-lasting topic : https://forum.unity.com/threads/detect-most-recent-input-device-type.753206/
I share here my discovery to avoid someone else spend time on the same issue.
To sum up the new input system is not 100% over : API is not complete and documentation is faulty or missing and here is the piece of code from Rene-Damm and FeastSC2 that solves my case. (Thank you guys)
InputSystem.onActionChange += (obj, change) =>
{
if (change == InputActionChange.ActionPerformed)
{
var inputAction = (InputAction) obj;
var lastControl = inputAction.activeControl;
var lastDevice = lastControl.device;
Debug.Log($"device: {lastDevice.displayName}");
}
};
Your answer
Follow this Question
Related Questions
Creating a menu for gamepad? 1 Answer
Unity Editor swallows gamepad input 0 Answers
Gamepad press and release in Input Action - UI Buttons 1 Answer