Search Unity

Bug Client local time is ahead of server local time?

Discussion in 'Netcode for GameObjects' started by RattusMakeGame, Feb 25, 2023.

  1. RattusMakeGame

    RattusMakeGame

    Joined:
    Feb 19, 2023
    Posts:
    3
    I'm attempting to help reduce bullet lag by spawning projectiles ahead of where they should be spawned based on their move speed and the average latency. However, the client's local time is almost always higher than the server's. Is there a different way I should be calculating latency, or perhaps am I not synchronizing the times correctly?

    Code (CSharp):
    1. void PrimaryFire()
    2.     {
    3.         double clientSendTime = NetworkManager.Singleton.NetworkTimeSystem.LocalTime;
    4.         ShootPrimaryServerRpc(clientSendTime);
    5.     }
    Code (CSharp):
    1. [ServerRpc]
    2.     public void ShootPrimaryServerRpc(double clientSendTime)
    3.     {
    4.         ///////////////////////////////////////////////
    5.         double latency = (NetworkManager.Singleton.NetworkTimeSystem.LocalTime - clientSendTime);
    6.         Debug.Log("Server's local time: " + NetworkManager.Singleton.NetworkTimeSystem.LocalTime);
    7.         Debug.Log("Client send time: " + clientSendTime);
    8.         Debug.Log("Latency: " + latency);
    9.     }
    clientSendTime is consistently higher and the latency variable is usually somewhere around -0.02 to -0.03. Thanks :)