Search Unity

Netcode Server Authoritative

Discussion in 'Multiplayer' started by paolo4523, Jan 3, 2023.

  1. paolo4523

    paolo4523

    Joined:
    Nov 28, 2021
    Posts:
    2
    Hi guys, I made a project with a car controller and I want to implement the multiplayer logic.

    First I used the client Transform Networks and everything worked fine, but now I would to use an authorative Server.

    In my script I have a function called Move:

    Code (CSharp):
    1.     private void MovePlayer()
    2.     {
    3.         if (!IsOwner) return;
    4.         GetInput();
    5.         HandleMotor();
    6.         HandleSteering();
    7.         UpdateWheels();
    8. }
    9.  
    10.     private void FixedUpdate()
    11.     {
    12.         if (IsOwner)
    13.         {
    14.             MovePlayer();
    15.         }
    16.     }
    17.  

    I'm not sure about how to procede, initially I tried to use the clientRPC but in any case the client does not move.
     
  2. paolo4523

    paolo4523

    Joined:
    Nov 28, 2021
    Posts:
    2
    Ok little update, I tried removing my script and using one based on the Character Controller and serverRPC and it actually works. This is a part of this script:

    Code (CSharp):
    1. void Update()
    2.     {
    3.         if (IsClient && IsOwner)
    4.         {
    5.             ClientInput();
    6.         }
    7.  
    8.         ClientMoveAndRotate();
    9.     }
    10.  
    11.     private void ClientMoveAndRotate()
    12.     {
    13.         if (networkPositionDirection.Value != Vector3.zero)
    14.         {
    15.             characterController.SimpleMove(networkPositionDirection.Value);
    16.         }
    17.         if (networkRotationDirection.Value != Vector3.zero)
    18.         {
    19.             transform.Rotate(networkRotationDirection.Value, Space.World);
    20.         }
    21.     }
    22.  
    23.     private void ClientInput()
    24.     {
    25.         // left & right rotation
    26.         Vector3 inputRotation = new Vector3(0, Input.GetAxis("Horizontal"), 0);
    27.  
    28.         // forward & backward direction
    29.         Vector3 direction = transform.TransformDirection(Vector3.forward);
    30.         float forwardInput = Input.GetAxis("Vertical");
    31.         Vector3 inputPosition = direction * forwardInput;
    32.  
    33.         // let server know about position and rotation client changes
    34.         if (oldInputPosition != inputPosition ||
    35.             oldInputRotation != inputRotation)
    36.         {
    37.             oldInputPosition = inputPosition;
    38.             UpdateClientPositionAndRotationServerRpc(inputPosition * walkSpeed, inputRotation * rotationSpeed);
    39.         }
    40.     }
    41.  
    42. [ServerRpc]
    43.     public void UpdateClientPositionAndRotationServerRpc(Vector3 newPosition, Vector3 newRotation)
    44.     {
    45.         networkPositionDirection.Value = newPosition;
    46.         networkRotationDirection.Value = newRotation;
    47.     }

    In this case networkPositionDirection and networkRotationDirection are NetworkVariables.



    Now, my question is, how I can move a player not based on the Character Controller, but based on stuff like MotorTorque and WheelColliders? I have to pass them like NetworkVariables?