Search Unity

Question Multiplayer with New Input System

Discussion in 'Netcode for GameObjects' started by CptMarvel21, Apr 14, 2022.

  1. CptMarvel21

    CptMarvel21

    Joined:
    Sep 18, 2021
    Posts:
    12
    I've been trying implement a player controller using the new input system but I cant figure out why it doesn't work. I've even tried referencing Dapper Dino's code from his Mirror series and tried to make it work for Unity's MLAPI, and it still doesn't work. Only the Host has the controls, but the client's controls don't work.

    Code (CSharp):
    1. using System;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5. using UnityEngine.InputSystem;
    6. using Unity.Netcode;
    7.  
    8. public class PlayerController : NetworkBehaviour
    9. {
    10.     public InputActions inputActions;
    11.     public CharacterController controller;
    12.     //public PlayerManager playerManager;
    13.  
    14.     public InputActions InputActions
    15.     {
    16.         get
    17.         {
    18.             if (inputActions != null) { return inputActions; }
    19.             return inputActions = new InputActions();
    20.         }
    21.     }
    22.  
    23.     public override void OnNetworkSpawn()
    24.     {
    25.         if (IsLocalPlayer)
    26.         {
    27.             enabled = true;
    28.             InputActions.Player.Movement.performed += Movement_Performed;
    29.         }
    30.     }
    31.    
    32.     public void OnEnable()
    33.     {
    34.         if (IsLocalPlayer)
    35.         {
    36.             InputActions.Player.Enable();
    37.             //playerManager = GetComponent<PlayerManager>();
    38.             controller = GetComponent<CharacterController>();
    39.         }
    40.     }
    41.  
    42.     public void OnDisable()
    43.     {
    44.         InputActions.Player.Disable();
    45.     }
    46.  
    47.     private void Movement_Performed(InputAction.CallbackContext context)
    48.     {
    49.         Vector2 inputVector = context.ReadValue<Vector2>();
    50.         Debug.Log("I Should Have Moved: " + inputVector);
    51.         controller.transform.Translate(inputVector.x, 0, inputVector.y);
    52.     }
    53. }