If you’re using the Input System 1.0, there’s a bit more documentation about how to manage multiple controllers and there’s one super useful doc right HERE.
You can set up a single variable for each controller (such as var gamepad_One; var gamepad_Two; etc.) and then setting up which is the latest gamepad to have been used. This method requires you to have all the controls set up in a single script which mean you got to have a “central” input manager of some sort.
You can set up an array of gamepads ( var allGamepads = Gamepad.all; ) and then compare to find which, in the array, is being used by each player in row. It’s kinda like the previous option, but easier to use as you can just, then, use integer and array to point out the source (like allGamepads[1] from the code example above if the player One uses the second registered gamepad.) This, again, requires you to create a sort of “central” input manager that manage all inputs done by the players in a single script.
If you prefer an approach that has a bit more “adaptability”, you could make it so that each player’s prefab or character has their own Player Input component pre-attributed and directly target those Player Input instances from each individual player prefab instantiated. (Each players’ instance has its own controls script and its own Player Input component. When instantiating the player’s instance, you set up the variable in its control script so that it knows which gamepad should control its Player Input.
This last approach is great, but involve a bit more work in managing the input as you need to make sure things are done in the proper order. For example, you could pre-register the actual Gamepad (like in the first example above) on a general script and then, when needed, attribute each gamepad to each player’s instance as they are instantiated. It’s a bit of a work-around, but it ensure that this remains under controls.
The one down side of all of those approaches comes from the potential of a really simple, but bad situation.
What happens if a player… unplug a controller mid-game?
From that, it’s possible check when a device is unplugged and when a device is plugged back in.
If the same device is plugged back in, there’s nothing you need to do as the registered Gamepad variable is actually link to the gamepad unique number and not just as a generic gamepad. (You might get some error though depending on how you read the unplugged gamepad while it’s unplugged.)
In the Devices doc of the Input System, here’s the text that explains it:
The Input System keeps track of disconnected Devices in InputSystem.disconnectedDevices. If one of these Devices reconnects later, the Input System can detect that the Device was connected before, and reuses its InputDevice instance. This allows the PlayerInputManager to reassign the Device to the same user again.
The thing is more complex when it’s a case where a player actually change his controller. This is a possible action if the player was using a certain controller that ran out of battery and don’t have any spare batteries nor a way to charge/plug it. Maybe the player will, then, switch to a wired controller which, ultimately, is different.
For this, you got to do some quick check with something like shown in the link above:
InputSystem.onDeviceChange +=
(device, change) =>
{
switch (change)
{
case InputDeviceChange.Added:
// New Device.
break;
case InputDeviceChange.Disconnected:
// If this is happening, activate some boolean that will tell the game that a controller is now "missing".
break;
case InputDeviceChange.Connected:
// Plugged back in.
break;
case InputDeviceChange.Removed:
// Remove from Input System entirely; by default, Devices stay in the system once discovered.
break;
default:
// Always includes a default case for when a unused case is being called. Leave it empty.
break;
}
}
As you can see, you can get whenever a device got plugged back in, when a new device was plugged in and when one is disconnected. Now, how you set up to relink with a new device depends on how you set them up in the first place. You got to have some kind of menu/pause/warning that activates when the case InputDeviceChange.Disconnected; is being activate/called from which you can get the player with the missing controller to press a button on the new controller to set it up and replace the disconnected controller.