Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice

Question Can't figure out why my Client is slower than Hosts

Discussion in 'Netcode for GameObjects' started by Tauntastic, Mar 5, 2022.

  1. Tauntastic

    Tauntastic

    Joined:
    Sep 24, 2020
    Posts:
    17
    The Problem
    I can't figure out why my client is much slower than the hosts, where the host has the correct speeds.

    My logic

    The client and client owner reads input, the input is then sent to a function where it calculates new positions based on Time.fixedDeltaTime and then those are sent to the server via ServerRpc. Afterwards, the client fetches the data and updates the transforms of the objects.

    Thanks in advance.

    Code (CSharp):
    1.     private void Update()
    2.     {
    3.         if (IsClient & IsOwner) ClientInput();
    4.     }
    5.     private void FixedUpdate()
    6.     {
    7.         if (IsClient & IsOwner) ClientToServerUpdate();
    8.         ServerToClientUpdate();
    9.     }
    10.     private void ClientInput()
    11.     {
    12.         _mousePosition = Input.mousePosition;
    13.         _forward = Input.GetKey(KeyCode.W) || Input.GetKey(KeyCode.UpArrow);
    14.         _back = Input.GetKey(KeyCode.S) || Input.GetKey(KeyCode.DownArrow);
    15.         _left = Input.GetKey(KeyCode.A) || Input.GetKey(KeyCode.LeftArrow);
    16.         _right = Input.GetKey(KeyCode.D) || Input.GetKey(KeyCode.RightArrow);
    17.     }
    18.     private void ServerToClientUpdate()
    19.     {
    20.         _tfBase.position = ServerBasePosition.Value;
    21.         _tfBase.rotation = ServerBaseRotation.Value;
    22.         _tfTower.rotation = ServerTowerRotation.Value;
    23.     }
    24.     private void ClientToServerUpdate()
    25.     {
    26.         float baseRotationAngle = 0;
    27.         Vector3 basePosition = _tfBase.position;
    28.         Quaternion baseRotation = _tfBase.rotation;
    29.         Quaternion towerRotation = _tfTower.rotation;
    30.  
    31.         if (_forward) basePosition = Vector3.MoveTowards(basePosition, basePosition + _tfBase.right, moveSpeed * Time.fixedDeltaTime);
    32.         if (_back) basePosition = Vector3.MoveTowards(basePosition, basePosition - _tfBase.right, moveSpeed * Time.fixedDeltaTime);
    33.  
    34.         if (_left) baseRotationAngle = -90;
    35.         if (_right) baseRotationAngle = 90;
    36.         if (_left || _right)
    37.         {
    38.             Quaternion targetBaseRotation = Quaternion.Euler(new Vector3(0, baseRotationAngle, 0));
    39.             baseRotation = Quaternion.RotateTowards(baseRotation, baseRotation * targetBaseRotation, baseRotationSpeed * Time.fixedDeltaTime);
    40.         }
    41.  
    42.         if (Physics.Raycast(_camera.ScreenPointToRay(_mousePosition), out RaycastHit hit, float.MaxValue, layerMask))
    43.         {
    44.             Quaternion targetTowerRotation = Quaternion.LookRotation(hit.point - _tfBase.position);
    45.             targetTowerRotation = Quaternion.Euler(new Vector3(0, targetTowerRotation.eulerAngles.y - 90, 0));
    46.             towerRotation = Quaternion.RotateTowards(towerRotation, targetTowerRotation, towerRotationSpeed * Time.fixedDeltaTime);
    47.         }
    48.  
    49.         if (_oldPosition != basePosition || _oldRotation != baseRotation || _oldTowerRotation != towerRotation)
    50.         {
    51.             _oldPosition = basePosition;
    52.             _oldRotation = baseRotation;
    53.             _oldTowerRotation = towerRotation;
    54.  
    55.             UpdateClientTransformServerRpc(basePosition, baseRotation, towerRotation);
    56.         }
    57.     }
    58.  
    59.     [ServerRpc]
    60.     void UpdateClientTransformServerRpc(Vector3 position, Quaternion rotation, Quaternion towerRotation)
    61.     {
    62.         ServerBasePosition.Value = position;
    63.         ServerBaseRotation.Value = rotation;
    64.         ServerTowerRotation.Value = towerRotation;
    65.     }
     
  2. Tauntastic

    Tauntastic

    Joined:
    Sep 24, 2020
    Posts:
    17
    Client Print:
    ServerDeltaTime: 0.01594734 -- MoveSpeed: 1 -- BaseRotationSpeed: 50 -- TowerRotationSpeed: 65


    Host Print:
    ServerDeltaTime: 0.01667547 -- MoveSpeed: 1 -- BaseRotationSpeed: 50 -- TowerRotationSpeed: 65
     
  3. Finn_O

    Finn_O

    Joined:
    Nov 18, 2020
    Posts:
    11
    It seems too me, as if your client is already calculating the next movement before changing its own position. This is still done from the old position and only a bit later it really gets moved. This way your client calculates the same step multiple time and is therefor slower. The host is at the correct speed since it updates its position without delay.

    You could safe an extra version of your position and rotations on the client and calculate according to these. This way you can use the current values.