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

Question Multiplayer movement lerp

Discussion in 'Multiplayer' started by JohnyBro, May 21, 2020.

  1. JohnyBro

    JohnyBro

    Joined:
    Dec 22, 2014
    Posts:
    4
    Hi guys,

    Im working on a P2P multiplayer game. Right now Im doing the movement of players over the network.
    I followed the tips from this post for the lerp option with fixed position interval.

    In my case Im sending positions 20 times per seconds.
    Here is my code for sending the position.
    Code (CSharp):
    1.  
    2.     private void LateUpdate()
    3.     {
    4.         //Trigger the position and rotation update event every PositionUpdateRate per second
    5.         if(TimeFromLastPositionUpdate >= (1f / PositionUpdateRate.Value))
    6.         {
    7.             OnPlayerPositionUpdate.Raise(transform.position);
    8.             OnPlayerRotationUpdate.Raise(transform.rotation);
    9.             TimeFromLastPositionUpdate = 0f;
    10.         }
    11.         else
    12.         {
    13.             TimeFromLastPositionUpdate += Time.deltaTime;
    14.         }
    15.     }
    16.  
    This code work properly, now when I receive those update I put them in a buffer, the buffer is a Queue<>.
    Code (CSharp):
    1.  
    2.     /// <summary>
    3.     /// Triggered when the command "position" is received from the network
    4.     /// </summary>
    5.     /// <param name="strData"></param>
    6.     public void UpdatePosition(string strData)
    7.     {
    8.         CmdPositionUpdate command = JsonUtility.FromJson<CmdPositionUpdate>(strData);
    9.         PositionsBuffer.Enqueue(command);
    10.     }
    11.  
    Now the fun part, each update I check if the buffer isn't empty and I start to lerp from positions to positions in my buffer, the thing is the time a lerp take between two position should be the same as the position interval rate sent over the network so the buffer size is constant.
    But it's not the case, sometimes the buffer grow over time and sometimes not, here is the code called each update.
    Code (CSharp):
    1. private void UpdatePositionWithLerp()
    2. {
    3.     //Each frame we move toward the target position, we calculate the PositionLerpProgress based
    4.     //on the update rate, we use a buffer so the player don't stop if the packet is delayed on the network
    5.     Debug.Log(PositionsBuffer.Count);
    6.     //We wait until the buffer is full
    7.     if (PositionsBuffer.Count >= BufferSize)
    8.     {
    9.         //If we reached our target position go to the next
    10.         if (PositionLerpProgress >= 1)
    11.         {
    12.             Debug.Log("Finished a lerp");
    13.             PositionLerpProgress = 0f;
    14.             StartPosition = transform.position;
    15.             TargetPosition = PositionsBuffer.Dequeue().Position;
    16.         }
    17.  
    18.         PositionLerpProgress += Time.deltaTime / (1f / PlayerPositionUpdateRate.Value);
    19.         transform.position = Vector3.Lerp(StartPosition, TargetPosition, PositionLerpProgress);
    20.     }
    21. }
    I don't understand why sometimes my buffer grow indefinitely whick lead to increasing delay and sometimes not, any help is appreciated and let me know if you need more informations.