Search Unity

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

Resolved Client Object Jumps On Server Side Only When Interacted With By Client

Discussion in 'Netcode for GameObjects' started by KurisC, Jan 26, 2024.

  1. KurisC

    KurisC

    Joined:
    Feb 9, 2021
    Posts:
    14
    I have created a short video to demonstrate this:


    On the server side, the Server can correctly engage with their own (top) panel, as soon as the client does the same, it remains in the correct position on the client but bounces to the middle of the map on the server side only.

    Here is the Script for that:
    Code (CSharp):
    1. using UnityEngine;
    2. using Unity.Netcode;
    3.  
    4. public class PlayerMovement : NetworkBehaviour
    5. {
    6.     // MOVEMENT VARIABLES //
    7.     private float _horizontalInput;
    8.     private float _moveSpeed;
    9.     private float _minXClamp;
    10.     private float _maxXClamp;
    11.  
    12.     private void Start()
    13.     {
    14.         _moveSpeed = 5f;
    15.         _minXClamp = -9f;
    16.         _maxXClamp = 9f;
    17.     }
    18.  
    19.     private void Update()
    20.     {
    21.         if (IsOwner)
    22.         {
    23.             _horizontalInput = Input.GetAxis("Horizontal");
    24.  
    25.             if (_horizontalInput != 0f)
    26.             {
    27.                 transform.position = new Vector3(
    28.                     Mathf.Clamp(transform.position.x + _horizontalInput * _moveSpeed * Time.deltaTime, _minXClamp, _maxXClamp),
    29.                     transform.position.y,
    30.                     transform.position.z
    31.                 );
    32.             }
    33.         }
    34.     }
    35. }

    And, the short script for the Client Network Transform
    Code (CSharp):
    1. using Unity.Netcode.Components;
    2.  
    3. namespace Unity.Multiplayer.Samples.Utilities.ClientAuthority
    4. {
    5.     public class ClientNetworkTransform : NetworkTransform
    6.     {
    7.         protected override bool OnIsServerAuthoritative()
    8.         {
    9.             return false;
    10.         }
    11.     }
    12. }

    They are both using the same Prefab, with the following Network related components.


    I have attempted to turn off syncing the Y position, it does not work, I have attempted to use a Network variable to attempt to sync the Y position, and it still does not work.
     
  2. KurisC

    KurisC

    Joined:
    Feb 9, 2021
    Posts:
    14
    Debugging the Update loop to simply display the OwnerClientId and the transform.position of the object shows that it is in the correct position:


    Until touched by the client:


    At which point, the y position incorrectly changes to 0 for the client prefab.
     
  3. KurisC

    KurisC

    Joined:
    Feb 9, 2021
    Posts:
    14
    Also adding on this script which handles spawning the players in their positions:
    Code (CSharp):
    1. using UnityEngine;
    2. using Unity.Netcode;
    3.  
    4. public class SpawnPlayers : NetworkBehaviour
    5. {
    6.     [SerializeField] private GameObject _playerPrefab;
    7.  
    8.     private void Start()
    9.     {
    10.         if (IsServer)
    11.         {
    12.             GameObject _spawnedPlayer = Instantiate(_playerPrefab, new(0, 5.2f, 0), Quaternion.identity, gameObject.transform);
    13.             _spawnedPlayer.GetComponent<NetworkObject>().SpawnAsPlayerObject(NetworkManager.LocalClient.ClientId);
    14.         }
    15.  
    16.         else
    17.         {
    18.             SpawnPlayerRpc(NetworkManager.LocalClient.ClientId);
    19.         }
    20.     }
    21.  
    22.     [Rpc(SendTo.Server)]
    23.     private void SpawnPlayerRpc(ulong _clientId)
    24.     {
    25.         GameObject _spawnedPlayer = Instantiate(_playerPrefab, new(0, -5.2f, 0), Quaternion.identity, gameObject.transform);
    26.         _spawnedPlayer.GetComponent<NetworkObject>().SpawnWithOwnership(_clientId);
    27.     }
    28. }