- Home /
How to assign a determined joystick number to each specific gamepad?
Hi everybody
I'm working in a project that uses 3 different types of gamepads, type A, B and C. I'm using Unity's Input Manager to handle the inputs. I'm setting the axes and assigning each a specific "Joy Num". I was expecting the joystick A would be recognized in unity as joystick 1, B would be joystick 2 and C would be joystick 3 (Following the order each gamepad was connected). The problem is sometimes unity works as expected, however there are times it doesn't. Gamepad A gets tag 2, B gets 3 and C gets 1, for example. Or a gamepad can get a tag 4 or 5, even there are only 3 gamepads connected. There is no way of predicting which "joy num" will be assigned to each gamepad.
Is there any way of changing the "Joy Num" tag in Input Manager on the run by script? Or is there any kind of workaround for this problem?
Thanks for your help
Answer by erizzoalbuquerque · Oct 09, 2015 at 09:06 PM
We got a solution! We're using Unity API to detect each joystick name and then we write the InputManager.asset text file by code and do the parsing (Since Unity3d doesn't offer any API for doing so).
Answer by mcarrascosh · Nov 24, 2017 at 10:43 PM
Hey @erizzoalbuquerque I'm having the same problem and have no idea how to solve it. Could you please provide more specific info about your solution? Greetings!
Answer by Kadaj · Mar 27, 2019 at 12:57 PM
Hello @mcarrascosh, since the time, have you found a solution to solve this? Thanks.
Hey @$$anonymous$$adaj! This is what I did:
for (int action = (int)$$anonymous$$eyCode.Backspace; action <= (int)$$anonymous$$eyCode.Joystick8Button19; action++)
{
if (Input.Get$$anonymous$$eyDown(($$anonymous$$eyCode)action) && (($$anonymous$$eyCode)action).ToString().Contains("Joystick"))
{
controllerNumber = (($$anonymous$$eyCode)action).ToString().Substring(8, 2);
if (controllerNumber.EndsWith("B"))
{
controllerNumber = controllerNumber.Substring(0, 1);
}
Debug.Log("This is Joystick Number " + controllerNumber);
}
}
If you press the Button 0 on your first controller, then the corresponding $$anonymous$$eyCode will be "Joystick1Button0". Getting the the substring of that you can read the specific number of the joystick as a string. You can then get an int for that string with Convert.ToInt32(controllerNumber). It's not very elegant and probably not efficient at all (especially if your calling this on a loop), but it worked perfectly for me!
Very nice! Thank you @mcarrascosh for your sharing.