Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Synchronizing players in Unity's Netcode and relay. latency troubleshoot

Discussion in 'Multiplayer' started by ALIENPANDA, Jun 29, 2023.

  1. ALIENPANDA

    ALIENPANDA

    Joined:
    Jan 14, 2020
    Posts:
    31
    I am currently developing a multiplayer racing game in Unity that utilizes unity's netcode for gameobject and relay. However, I have encountered a problem with the synchronization between two connected devices/build. Specifically, when both devices/build players press the play button, Player A appears to be ahead of Player B on Device/Build One, while on Device/build Two, Player B is ahead of Player A. This syncing issue is causing inconsistencies between the two devices. How do I resolve this problem?

     
  2. JesOb

    JesOb

    Joined:
    Sep 3, 2012
    Posts:
    1,081
    I'm not expert not in syncing racing games nor in netcode for game objects :)

    But some time ago was write such system for mobile racing game.

    The only idea how to sync cars that very close to each other on track was to sync not only car itself but also delta distance and delta velocity between cars. So each player want to have those delta distance and delta velocity for each pretty close ghost cars on track to show it on right place and semicorrect extrapolate current position.

    May be you can find good article about syncing exactly networked racing games, because it is one of the most challenging type of syncing among game genres.
     
  3. Lukeesta

    Lukeesta

    Joined:
    Jan 7, 2016
    Posts:
    64
    This is not easy to solve. What you need is "client side prediction" or more accurately "full world physics prediction" basically to put the other car in the same timeframe as yours you need to simulate it ahead of time by guessing inputs. This is not something that Netcode for GameObjects provides out of the box. You have to either implement it yourself or use a library which supports it out of the box.
     
  4. ALIENPANDA

    ALIENPANDA

    Joined:
    Jan 14, 2020
    Posts:
    31
    This is basically a simple game. I'm only using transform.position to move the object on Z axis. I'm not using any physics here. So how do i achieve this using this delta distance or delta velocity here? Can you share some reference or anything to solve this?
     
  5. ALIENPANDA

    ALIENPANDA

    Joined:
    Jan 14, 2020
    Posts:
    31
    I tried this first with the photon pun2. Since, it was showing the exact same issue, i thought i will give a shot in the unity's netcode. No difference. I made some research on the client side prediction, but I'm not getting how to implement it. Any suggestion on how do i implement it. Currently Im not using any physics here. Just transform.position to move the object on Z Axis alone.
     
  6. Lukeesta

    Lukeesta

    Joined:
    Jan 7, 2016
    Posts:
    64
    PUN2 also can't do this. We have Fusion and Quantum at Photon which both provide this out of the box and give you a very nice racing experience.
    https://assetstore.unity.com/packages/templates/tutorials/fusion-karts-multiplayer-213885
    https://doc.photonengine.com/quantum/current/game-samples/arcade-racing/overview
     
    JesOb likes this.
  7. ALIENPANDA

    ALIENPANDA

    Joined:
    Jan 14, 2020
    Posts:
    31
    I am currently experimenting with the photon fusion and its fusion karts. Transitioning from Pun2 and Unity Netcodes, I find the code to be quite intricate. Specifically, I'm facing a challenge regarding the kart's movement, which relies solely on keyboard input obtained through the new input system. I'm trying to move the Kart using a method in the KartEntity script, where I increment the AppliedSpeed variable. However, it functions correctly for the host, but it fails to work for the client. Can you help me in solve this issue?

    Code (CSharp):
    1.     private void Move(KartInput.NetworkInputData input)
    2.     {
    3.         if (input.IsAccelerate)
    4.         {
    5.             AppliedSpeed = Mathf.Lerp(AppliedSpeed, MaxSpeed, acceleration * Runner.DeltaTime);
    6.         }
    7.         else if (input.IsReverse)
    8.         {
    9.             AppliedSpeed = Mathf.Lerp(AppliedSpeed, -reverseSpeed, acceleration * Runner.DeltaTime);
    10.         }
    11.         else
    12.         {
    13.             AppliedSpeed = Mathf.Lerp(AppliedSpeed, 0, deceleration * Runner.DeltaTime);
    14.         }
    15.  
    16.         var resistance = 1 - (IsGrounded ? GroundResistance : 0);
    17.         if (resistance < 1)
    18.         {
    19.             AppliedSpeed = Mathf.Lerp(AppliedSpeed, AppliedSpeed * resistance, Runner.DeltaTime * (IsDrifting ? 8 : 2));
    20.         }
    21.  
    22.         // transform.forward is not reliable when using NetworkedRigidbody - instead use: NetworkRigidbody.Rigidbody.rotation * Vector3.forward
    23.         var vel = (Rigidbody.rotation * Vector3.forward) * AppliedSpeed;
    24.         vel.y = Rigidbody.velocity.y;
    25.         Rigidbody.velocity = vel;
    26.     }
    here if i try to add a value to AppliedSpeed variable, the kart moves accurately for the host but not for the client.
     
  8. Lukeesta

    Lukeesta

    Joined:
    Jan 7, 2016
    Posts:
    64
    There could be multiple causes for this. The code snippet you shared itself looks correct so it's likely an issue with the surrounding code.

    1. Is this called from inside FixedUpdateNetwork and inside a `if(GetInput)` clause?
    2. Is AppliedSpeed a [Networked] Property? It has to since you are storing it between FixedUpdateNetworks and thus Fusion needs to be able to rollback correctly.
    3. Does this object have a NetworkRigidbody on it?

    If the above 3 are all correct then it might be an issue with how you spawn the player object as well.

    And one more thing. Check in the NetworkProjectConfig that the physics mode is set to ClientPredicted.