Pong-like football game - best practice for synchronizing

Options
Hi, I have simple scene with two football players and one ball. Players can move across the field and they interact with ball by bouncing. Each player can also kick the ball and this is implemented through computing certain force vector and applying this vector to the ball when player press button. All three objects have RigidBody2D and PhotonTransformView component, Ball has also PhotonRigidBody2DView.

I am currently implementing multiplayer version of this game with Unity and Photon and I´m struggling with some problems regarding kicking. My current implementation looks like this:
1) Compute force vector based on common position of the ball and the player when the ball is close enough
2) If player press kick button and he is close enough to ball to kick:
2a) kicking player requests ownership of the ball gameObject (ownership set to Takeover on ball object)
2b) kicking player sends RPC with final vector to other clients (PhotonTargets.AllViaServer) and the force is applied to the ball

It is working well in 90% of the cases, but sometimes when the player without ball ownership kicks the ball, the ball moves little bit to desired direction and then stops. After this step player gets an ownership and all the other kicks are working well.

Could you please give me some advice regarding my problem? It is really annoying because it is hard to replicate the problem and I don`t know what can cause it.

Also, some more general question - is there some best practice recommendation how to deal with realtime physics-driven ball shared with the players on the scene?

Every advice is welcomed, thanks in advance!

Comments

  • jeanfabre
    Options
    Hi,

    I would say don't change ownership and keep the ball a scene object that belongs to the masterClient, and simply send RPCs to kick the ball, and only the master is eligible to control the ball, regardless of what Player is the master.

    Does that make sense?

    Bye,

    Jean
  • crabMan
    Options
    Hi, thanks for your response!

    This was initially my first solution, but the problem is that kick doesn´t work for regular client (master client is ok) - the problem I´m experiencing now (ball moves in desired direction, then immediately stops) is permanent at client. So I´ve started to changing ownership and the problem is mostly gone, but sometimes it is still happening.

    I´m just thinking it could be related to physics settings on player/ball prefabs maybe?
  • jeanfabre
    Options
    Hi,

    you should try again, It's definitly possible to give authority to the masterClient for controlling object on behalf of player input, you may have not sent the RPC properly :)

    Indeed it could be some clashes and on a player that is not the master, physics still tries to fight where the photonView tells it to go, it's very probable too.

    Bye,

    Jean
  • crabMan
    crabMan
    edited November 2016
    Options
    In the end I found a root of my problem - weird ball behaviour was caused by bad time synchronization between clients. Our game is time limited and when the time was too different on the clients, we´ve experienced behaviour described above. After I set start of the match on the same time on both clients, problems were gone...
  • jeanfabre
    Options
    Hi,

    Good, I am glad it worked out in the end.

    Bye,

    Jean