- Home /
Mapping multiple controllers
Hello,
My current input manager is set up to read each button on up to four gamepads, as they are mapped 'joystick 3 button 0' as "A_P3", for example.
My goal is to allow up to four players to join in a local game by pressing the 'A' button on their gamepad. The issue I can't seem to work around is that if the players use a USB hub device to extend gameplay up to four players, Unity won't read the joystick as say 'joystick 4'. Also, it doesn't seem that if I try using a global set of buttons for all gamepads would work, as I don't want it to conflict with the players already in game.
If you have any solution to my issue, any feedback would be much appreciated. Thank you.
tl;dr I am trying to map gamepads regardless if a USB hub is used, or use one input manager setting available up to four controllers
is there perhaps a way to store a controller that's plugged in as a variable?
Answer by darbotron · Dec 04, 2016 at 07:17 PM
This is fixable using the standard UnityEngine.Input.xxx functions (my own unity based framework does this), but it requires a significant amount of code & is non-trivial to explain in an answer here.
If you want to be able to just get a "controller" as a variable, you need to wrap the Input library in a code interface that behaves the way you want.
Buttons are easy, since you can access the buttons from a specific joystick with either the string name or a value from the keycode enum ( https://docs.unity3d.com/ScriptReference/KeyCode.html).
Axes are a bit more difficult as you have to define them by hand in the InputManager pane in the editor. You need to make axes for each possible controller number and name them in a procedural manner so you can easily use the same interface as for the buttons.
e.g. you might call the up/down axis of the left stick on pad 1 "Pad1_LS_Vertical", on pad 2 "Pad2_LS_Vertical" ...etc. you could then use string.Format( "Pad{0}_LS_Vertical", padNumber ) to generate the string to use with Input.GetAxis( ... ).
If you're after a quick solution might be to get an asset store package which does this for you. I hear InControl (www.assetstore.unity3d.com/en/#!/content/14695) is a decent one - lots of shipped unity games use it...