Search Unity

  1. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Resolved Network Transform desync after short rapid movement.

Discussion in 'Netcode for GameObjects' started by GoxYaZK, Aug 29, 2023.

  1. GoxYaZK

    GoxYaZK

    Joined:
    Aug 13, 2022
    Posts:
    4
    Hi, the network transforms desync and I cannot understand why. In the gif I attached you can see how after short movement, when the player stops moving, the other clients do not have the correct position. Sometimes after it has desynced, if you move it syncs again, but I don't like that it desyncs at all. This happens only when interpolation is turned on. When it is turned off there is no desync.

    Desyncing.gif

    Still I tried the following:
    Make it server authoritative, didn't work. Checked all movement calculations if I have missed any addition of Time.delta time and the different FPS caused it to desync, but all include it. I removed the rigid body, maybe it's the physics, still desyncs, the players in the gif do not contain a rigid body. I tried sending a small movement to the clients to force the transform to sync, but that also didn't work, for some reason it updates in intervals and not move directly to the correct place. I also tried it in 3D (maybe there is some difference?), didn't work either. Tried from different computers, with VPNs on and off (with relay). Still desyncs. Also tried LateUpdate, but doesn't work either.

    The player contains only a sprite renderer, network object, client network transform and a movement script.
    This is the movement script of the player.
    Code (CSharp):
    1. private float playerSpeed = 7f;
    2. private Transform playerTransform;
    3.  
    4. void Update()
    5. {
    6.     if (!IsOwner) return;
    7.     Vector3 moveDir = new Vector3(0, 0, 0);
    8.  
    9.     if (Input.GetKey(KeyCode.W)) moveDir.y = +1f;
    10.     if (Input.GetKey(KeyCode.S)) moveDir.y = -1f;
    11.     if (Input.GetKey(KeyCode.A)) moveDir.x = -1f;
    12.     if (Input.GetKey(KeyCode.D)) moveDir.x = +1f;
    13.  
    14.     Move(moveDir);
    15. }
    16.  
    17. private void Move(Vector3 input)
    18. {
    19.     transform.position += input * playerSpeed * Time.deltaTime;
    20. }
    I am using Netcode for GameObjects version 1.4.0

    What I think is happening is the last few packets are not being sent or received therefore the clients desyncs.

    I'm really confused why its happening and would greatly appreciate if someone can help me.

    FIXED!
    The setting Use Half Point Precision fixes it for some reason.
     
    Last edited: Oct 22, 2023
  2. CodeSmile

    CodeSmile

    Joined:
    Apr 10, 2014
    Posts:
    4,992
    Try running the above code in LateUpdate(). If that doesn't help, try replacing deltaTime with a constant value for testing.

    You already figured out that the issue is related to interpolation. So it seems likely to be either a bug with Interpolation or a timing issue.
     
  3. GoxYaZK

    GoxYaZK

    Joined:
    Aug 13, 2022
    Posts:
    4
    I'm not sure how to replace deltaTime with a constant value, can you give elaborate a bit more? Also lateUpdate doesn't work, I tried before but forgot to mention it.
     
  4. CodeSmile

    CodeSmile

    Joined:
    Apr 10, 2014
    Posts:
    4,992
    Just like this (or omit it altogether): ;)

    transform.position += input * playerSpeed * 0.016f;
     
  5. GoxYaZK

    GoxYaZK

    Joined:
    Aug 13, 2022
    Posts:
    4
    Oh, I was thinking of something more complicated, didn't work though. Thanks either way.
     
  6. GoxYaZK

    GoxYaZK

    Joined:
    Aug 13, 2022
    Posts:
    4
    Okay update, just randomly decided to turn all of the settings on in the client network transform, last attempt before I give up completely. ... Somehow it works now.
    The setting Use Half Point Precision fixes it for some reason.
    So yay :)
     
    CodeNinja- likes this.