Clients are delayed due to latency

Options
Hello, I'm trying to make a pong game with PUN.
So far, I have two players with two paddles that move up and down and I have a ball that bounces around.
Now, I have a photonview attached to the paddles and the ball. I am smoothing their movement using the steps outlined in the Marco Polo tutorial.

The problem I'm experiencing is that the ball is like 300ms behind on the client. The host will see the ball in real time and can hit it and play a regular game of pong, and to the host, it looks like the client is doing fine too.

But on the client screen, the ball is moving a little funny, sometimes it bounces before it hits walls, sometimes it'll go straight through your paddle. I understand why this is happening due to latency, but I can't think of an easy way to fix it without having the ball somehow be "server-side."

Some proposed solutions I've seen are:

[*]Two separate ball instances on each device and use RPCs to let the other player know when a collision happens and sync up with that.
PROBLEM: The RPC is gonna be a little late due to network latency causing the ball on the other player's game to get out of sync.

[*]Predict where the ball will be based on velocity and account for latency that way.
PROBLEM: The ball will be really glitchy, because it's almost impossible to tell when the next network message arrives precisely enough for this calculation...


So what can I do? Does anyone have any suggestions for syncing this rigidbody ball?

Comments

  • Hello,

    I use prediction to smooth out the transforms via the network no matter what the ping.

    // FixedUpdate
    [code2=csharp]float ping = (float)(PhotonNetwork.GetPing() * 0.001);
    float lastTime = (float)(PhotonNetwork.time - this.m_lastNetworkDataTime);
    float totalTimePassed = ping + lastTime;

    Vector3 predictionPosition = this.m_streamPosition + this.transform.forward * this.m_streamSpeed * totalTimePassed;
    Vector3 newPosition = Vector3.Lerp(this.transform.position, predictionPosition, 0.1f);

    this.transform.position = newPosition;[/code2]

    First I calculate the ping in seconds.
    Then I get the ∆t between Serialization updates.
    Then I calculate the total time, ∆t + ping time.
    Based on the allowed time, and since I know the speed I predict where the position should be. Now, for me I can use transform.forward for the ball I don't know how you are moving it. I use rotations on the rigidbody and set the velocity.
    Now that I have what I predict, I Lerp fast between the current position and the predicted position. I then set that Vector3 to the current position.
    This has worked well for me, I don't get any jitter. This may or may not work for you.