Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

correct way to do lockstep

Discussion in 'Netcode for GameObjects' started by zh99998, Dec 1, 2021.

  1. zh99998

    zh99998

    Joined:
    Mar 2, 2020
    Posts:
    17
    I'm making a multiplayer TD game and trying this new netcode library.
    I want to use deterministic lockstep way. following this document added a tick callback
    https://docs-multiplayer.unity3d.com/docs/advanced-topics/networktime-ticks
    then do all game logic update on tick callback

    but seems the `Tick` is not sync on network, one client run faster than another.

    my code looks like:

    Code (CSharp):
    1.  
    2. public class HelloWorldPlayer : NetworkBehaviour
    3. {
    4.     private static int playerCount;
    5.     private static int i;
    6.  
    7.     public override void OnNetworkSpawn()
    8.     {
    9.         count++;
    10.         if (count == 2) NetworkManager.NetworkTickSystem.Tick += () =>
    11.         {
    12.              Debug.Log(i++);
    13.         };
    14.     }
    15. }
     
  2. scottalexgray

    scottalexgray

    Joined:
    May 14, 2013
    Posts:
    2
    This is because you always want your clients running at a tick higher than your server so that you can implement Client Side Prediction correctly (as far as I can tell). From my testing the NetworkManagers send each other corrections based on the network latency so that if a client send a packet to the server with say tick 100, and there is 5 ticks latency, then when the server receives the packet it is still processing tick 99, and won't discard the old tick.
    https://www.youtube.com/watch?v=itrGCVt3rOs
    https://www.youtube.com/watch?v=2Xl0oaTKBXo
     
    IggyZuk likes this.