- Home /
Check whether specific joystick connected
Does anyone know of a way to check whether a specific joystick number is connected to Unity? I'm trying to detect when a controller is disconnected from my multiplayer game, but Unity does not seem to give any access to the information about which joysticks are currently connected to which input numbers.
I'm familiar with Input.GetJoystickNames(), but the names aren't very useful (every controller I have is named "Controller (Xbox 360 Wireless Receiver for Windows)"), and the array shrinks to only return the names of the currently connected controllers. This means that, even if Unity is currently receiving input from Joystick 1 and Joystick 3 (because Joystick 2 was just disconnected), Input.GetJoystickNames().Length is 2, returning only information about the connected joysticks and no hint of which numbers they correspond to. There doesn't seem to be anything I can query to let my code know which joystick in particular just disconnected.
Does anyone know if it's possible to get this information?
I have no advice here! Just hitting the same problem. The unity editor seems to know (it tells you if a joystick is RE-CONNECTED, as opposed to just connected!), so it must internalize unique ids for each joystick of the same make?
Would love to know if there's some kinda standard script call on the $$anonymous$$onoBehaviour for this.
I never found an answer. The solution I ended up going with included tracking the maximum value I'd seen for Input.GetJoystickNames().Length, then pausing the game and prompting a reconnect of the controller if that number ever dropped.
I also have this issue, my solution for it was to just track the update if the position of the controllers changed each input, meaning it is plugged in... which isn't a very good solution at all.
Thanks! I was actually thinking about that. It's not perfect, but it's also not a terrible solution, since if you're getting the RAW axis value (i.e. without deadzone cropping tiny values around the origin), most joysticks are mechanically unstable, and will never return to a perfect 0,0. But if you read the raw axis of an axis which isn't there, I'm going to guess that you do get a perfect zero.
Basically, the chance of a plugged in analogue joystick having a raw axis value of zero on both (or all 10??) axes at once is negligible.
Obviously, it'd be better to have a proper fix with joystick UIDs being sent to monobehaviours when they're connected or disconnected, but it's pretty close.
Perhaps the richest and most efficient solution, then, would be a combination of both — catch that Input.GetJoystickNames().Length has dropped, then check each joystick in turn to find one returning raw 0s.
Your answer
Follow this Question
Related Questions
Problems with joystick / controller axes being 1/-1 "way too often" 1 Answer
problems moving game object with the right joystick 1 Answer
Using Input.GetJoystickNames 1 Answer
How do I make the Joystick Analog Stick work like the Mouse Input? 0 Answers
Why does pressing joystick button 0 also trigger joystick button 1? 1 Answer