I know about Input.GetJoystickNames() to get the current list of controllers connected to the system, and I know how to query for inputs. But what I don’t know how to do is determine whether a given joystick button press is coming from one controller or another.
Let’s assume that a player has connected both an XBox 360 controller and a Playstation controller into their system, and they start using one of the controllers. I’d like my UI to reflect the current controller’s button images. Is there any order to Input.GetJoystickNames()? Does it show most recently used first? Assuming a player starts my game with two joysticks connected, and presses a button on one of them, is there any way for me to know which controller it came from?
This is definitely a bit of an edge case, and I assume I’ll mainly be dealing with a single controller at a time, but if it’s simple I’d like my system to be robust.
This ended up being really simple. The answer, as far as I can tell, is that Input.GetKey(Up/Down) for joystick buttons can be used either to detect a button press on any connected controller (for example, JoystickButton1), or it can detect buttons presses on specific controllers (for example, Joystick1Button1, Joystick2Button1, etc). In the latter case, if the button press comes from Joystick1Button1, that means the input came from Input.GetJoysticks()[0]. If, instead, the input came from Joystick2Button1, then the input comes from nput.GetJoysticks()[1].
So, depending on the specific button press, it’s simple to map this back to the controller being used.
If you developing for consoles, I advice you to check out Unity’s new input system, there’s a thread on it in preview/betas forum section. Old system has major flaws like a unable to detect if controllers disconnected or connected and other. In the new system you just map inputs to actions and it works.
1 Like
I’ll keep an eye on it. I thought they weren’t officially supporting the new input system until Unity 2020.1, and my project is going to be stuck on Unity 2019.3 for the foreseeable future.
I’m on 2019.2 now and just checked package manager, it’s there. And this doc Installation guide | Input System | 1.0.2 says it requires 2019.1 minimum