How to implement local multiplayer with InputSystem

The basic idea is to divide the player by device, or even to divide the player within a single input device.
For example, separate players input WASD and Arrow keys on the keyboard.

I have created a scheme called WASD and Arrow,GamePad. Then the following code is going to generate a player and set up the scheme and device.
However, once I run PlayerInput.Instantiate, the joinAction of the other device stops responding.

    [SerializeField] InputActionReference joinAction;
    [SerializeField] PlayerInput playerPrefab;

    int currentPlayerCount;


    void Start()
    {
        joinAction.action.Enable();
        joinAction.action.performed += OnJoin;
    }

    private void OnDestroy()
    {
        joinAction.action.Dispose();
    }

    void OnJoin(InputAction.CallbackContext context)
    {
        InputDevice device = context.control.device;
        string scheme = GetScheme(context.control);

        PlayerInput.Instantiate(prefab: playerPrefab.gameObject,
            playerIndex: currentPlayerCount,
            controlScheme: scheme,
            pairWithDevice: device);

        currentPlayerCount++;
    }

    string GetScheme(InputControl control)
    {
        if (control.device is Gamepad) return "GamePad";

        if (control.name == "space")
            return "WASD";

        if (control.name == "enter")
            return "Arrow";

        return "Unknown";
    }```

Don’t DIY, at least for now. Use the PlayerInputManager instead. This automates multi-user device usage and you can inspect its code if you ever need to roll your own.

PS: Avoid getting schemes/controls by string. Either use the SendMessage approach which is super easy to use, or rely on the C# generated classes/interfaces for most flexibility and type safety.

I used PlayerInputManager, but it only lets me in on one device.
For example, after joining with WASD (left half of the keyboard), you can no longer join with GamePad.

The contents of InputActionAsset are the following settings.

ActionMap : PlayerControls
Actions : Interact

Scheme : WASD

Interact/Space[KeyBoard]

Scheme : Arrow

Interact/Enter[KeyBoard]

Scheme : GamePad

Interact/Button East[GamePad]

There must be something wrong because that’s definitely working for me and others.

Not sure if multiple users on the same device (keyboard) will work though. Perhaps this is what’s getting in the way, so give it a try without the secondary arrow controls. Keyboard/Gamepad switching should definitely work with the defaults, ie joining by pressing any key on a device.

1 Like

I managed to make it work without using arrow keys. However, since this is a local multiplayer game, I want to split the keyboard in half so that two players can play using a single keyboard.