Pong Game Lag Problem

L3sc
L3sc
Hello guys,
I'm working on pong game and I have some trouble.
I am synching paddle position from script(I found it on internet and best for what I tried for sync). Script attached below:
public Vector3 realPosition = Vector3.zero;
     public Vector3 positionAtLastPacket = Vector3.zero;
     public double currentTime = 0.0;
     public double currentPacketTime = 0.0;
     public double lastPacketTime = 0.0;
     public double timeToReachGoal = 0.0;
     
     void Update ()
     {
         if (!photonView.isMine)
         {
             timeToReachGoal = currentPacketTime - lastPacketTime;
             currentTime += Time.deltaTime;
             transform.position = Vector3.Lerp(positionAtLastPacket, realPosition, (float)(currentTime / timeToReachGoal));
         }
     }
 
     void OnPhotonSerializeView(PhotonStream stream, PhotonMessageInfo info)
     {
         if (stream.isWriting)
         {
             stream.SendNext((Vector3)transform.position);
         }
         else
         {
             currentTime = 0.0;
             positionAtLastPacket = transform.position;
             realPosition = (Vector3)stream.ReceiveNext();
             lastPacketTime = currentPacketTime;
             currentPacketTime = info.timestamp;
         }
     }

Ball has classic photon transform view with lerp option and also photon rigidbody view. Ball istantiating from script as scene object and default controlling by master client. I just update ball rb.velocity on start and when collision occur from who is master client. With this approach guest player sees game as reflection. When guest player is one corner of game area and trying to reach other corner for interact with ball. Guest is reaching that point on the time in own game but position sync is not one to one. So ball is gone outside game area on master client view when guest interact with ball. Then ball lagging on guest until come game area again.
What is the best approach for this situation? Do I need request ball ownership for each client before interact with the ball? If case is this which side must detect collision? Should the ball detect the collision again or the paddle?

Thanks in advance and sorry for my English.

Comments

  • Hi did you tried the PhotonTransformView component ? In your cases this should be enough to sync the padle properly.

    You coul also try https://github.com/SradnickDev/Photon-Template/blob/master/Assets/Utilities/StateBuffer.cs, it should work out of the box.

  • Hi,

    Your script doesn't do any smoothing on the receiving side, so indeed you will have a very bad result and jumps/jitter will happen due to the nature of the network traffic itself.

    You need to implement some lerping at least, or as I mentionned before, if your paddle has a constant speed movement, you may want to simply keep track of the user input and their timing and deduce the on the receiving side where the paddle goes instead of trybing to sync the position itself, but that's more demanding in terms of algorythm.

    Your best bet at this point is to use the PhotonTransformView component we provide.

    Bye,

    Jean