Search Unity

[Photon] Pong Ball not in sync between master and client

Discussion in 'Connected Games' started by noaim, Jul 17, 2017.

  1. noaim

    noaim

    Joined:
    Feb 27, 2015
    Posts:
    4
    Hey guys,

    currently I am working on a multiplayer pong game. For the communication between master and client I am using Photon. To transfer the movement of the paddles I am using the normal PhotonView and OnPhotonSerializedView but for the ball I had to use a different approach because if the ball has a huge speed it gets shaky and really inaccurate in his movement. That's why I tried to send the current position and velocity to the clients when the master recognized the ball hit a player or a deadzone. The information is send by RPC Calls. The client receive the information and try to imitate the physics on their own.

    On game start the ball is reset to the middle of the playground and the function GoBall is called.

    Code (CSharp):
    1. public void GoBall()
    2.     {
    3.         float rand = Random.Range(0.0f, 2.0f);
    4.         if (rand < 1.0f)
    5.         {
    6.             rb2d.AddForce(new Vector2(30.0f, -15.0f), ForceMode2D.Impulse);
    7.         }
    8.         else
    9.         {
    10.             rb2d.AddForce(new Vector2(-30.0f, -15.0f), ForceMode2D.Impulse);
    11.         }
    12.  
    13.         if (this.photonView.isMine)
    14.         {
    15.             this.photonView.RPC("onData", PhotonTargets.All, transform.position, rb2d.velocity);
    16.         }
    17.     }
    On the client this function gets called:

    Code (CSharp):
    1. [PunRPC]
    2.     public void onData(Vector3 position, Vector2 velocity)
    3.     {
    4.         Debug.Log("New Data: position: " + position.ToString() + "vel: " + velocity.ToString());
    5.         transform.position = Vector3.Lerp(transform.localPosition, position, Time.deltaTime * 5);
    6.         GetComponent<Rigidbody2D>().transform.position = Vector3.Lerp(transform.position, position, Time.deltaTime * 5);
    7.         this.GetComponent<Rigidbody2D>().velocity = velocity;
    8.     }
    I dont know why but somehow the imitation of the client is sometimes correct and sometimes totally wrong. Am I wrong with the way I set the velocity? Or do somebody have an idea what is wrong? :/

    Cheers
    NoAim
     
  2. tobiass

    tobiass

    Joined:
    Apr 7, 2009
    Posts:
    2,251
    Pong is an incredibly simple game and that makes it really tough to do as networked game. You basically can't hide lag or missing updates.
    If you send us a repro case, we might have a look at Thursday but I can't promise much.
    Using RPCs for the hits is good but they are reliable. If those get lost, they will lag and everything gets delayed.
     
  3. noaim

    noaim

    Joined:
    Feb 27, 2015
    Posts:
    4
    Hey thank you for your reply!

    I found 2 possible issues already. But there's still one thing which is weird. The bumping on the rackets is different on the clients itself. So what I think what I can do is sending the current position and current force to the client periodically and check if the position the master sent differs to much from the actual position on the client. If yes I will set everything and correct the client.

    I hope this works out
     
  4. vatsanpb

    vatsanpb

    Joined:
    Oct 28, 2015
    Posts:
    4
    Did it work out? Or did you find a way around?
     
  5. sckmkny

    sckmkny

    Joined:
    May 6, 2018
    Posts:
    2
    Wonder how many people think to build pong as their "hello world" networked game (with Photon) only to realize that synchronizing the movements (esp. the ball) is non-trivial. Messed with a number of the interpolation options, e.g., Lerp, fixed speed, estimated speed, all without providing both a smooth and accurate experience.

    The fundamental problem appears that it is hard to capture the rapid velocity change when the ball hits a surface. The good news, however, outside of hitting the surface the position is super predictable.

    Did some Googling and was surprised that I did not find a ton of articles on addressing this issue. Seems like something newbie game developers (like me) would bump into right away.

    Seems like the answer is to not rely on the Photon Transform View and do a more custom approach (as described above).
     
  6. sckmkny

    sckmkny

    Joined:
    May 6, 2018
    Posts:
    2
    tobiass likes this.
  7. tobiass

    tobiass

    Joined:
    Apr 7, 2009
    Posts:
    2,251
    You're spot on: The problem with the movement sync is a hard one and for Pong, it's effectively even harder than in most games: Everyone's eyes will be on the ball, which will bounce off a paddel or not, depending on the player's input. This can't be predicted and must be "solved" instantly, so the player does not feel the lag...

    Please keep us updated with your solutions.