How to use Input System together with Netcode for GameObjects

I was working on a new project of mine and wanted to use Netcode for GameObjects as it makes it quite easy to make a multiplayer game and the Input System package for how versatile it is, but I was having some problems on making them work together.
My code looked something like this:

public void Move(InputAction.CallbackContext context){
  MoveServerRpc(context.ReadValue<Vector2>(), new ServerRpcParams { Receive = new ServerRpcReceiveParams { SenderClientId = OwnerClientId} });
}

[ServerRpc]
public void MoveServerRpc(Vector2 input, ClientRpcParams @params){
//code to make the player move
MoveClientRpc(input, new ClientRpcParams { Send = new ClientRpcSendParams { TargetClientIdsNativeArray = ids} });
}

[ClientRpc]
public void MoveClientRpc(Vector2 input, ClientRpcParams @params){
//code to move the player on the client
}

Then, I assigned the Move() method to a UnityEvent inside the editor.
It looks and works just fine with one player, but then no other player that gets added have their inputs detected. After looking around, I understood that the problem I was having was happening because that same event only got assigned for the first prefab and consequently for the first PlayerInput instantiated.
My solution to make it work was to generate the Player Input Asset as a C# class, create an object of that same class inside my Controller script and subscribe each Action to my Move() method.
As a visualization, the code looks something like this:

private PlayerInputActions playerInput;

private void Start()
    {
        if (IsOwner)
        {
            playerInput = new PlayerInputActions();
            playerInput.Default.Enable();
            playerInput.Default.Move.performed += Move;
        }
    }

Because I didn’t find anyone talking about this (probably because of Netcode For GameObjects being so recent) I decided to make this post and I hope it helps anyone with the same problem in the future.

1 Like