1
votes

I am syncing a ball (like when the player kicks the ball, to shoot or pass). It works really well for the master client and is synced pretty well on the non-master clients as well. The issue is when any non-master client kicks the ball, the sync is not accurate enough and the ball behaves in a weird way for all clients connected to a room. I am using RPC calls when any player client kicks the ball to send the information to add force to the ball with respective direction and force parameters. This is the RPC call.

this.photonView.RPC("ShootBallRPC", RpcTarget.All, direction, height, directionPower, heightPower); 

This is the RPC Method.

private void ShootBallRPC(Vector3 direction, Vector3 height, float directionPower, float heightPower) { owner = null; rb.AddForce(((direction * directionPower) + (height * heightPower)), ForceMode.Impulse); }

I can't really understand what might be the issue here. One issue I can think of is adding the force in non FixedUpdate() so that may be resulting in weird ball movement when the client kicks the ball. Can anyone please help me out or give me some guidance on what alternate thing I can do to resolve or sort out such issue? Thanks!

"ball behaves in a weird way": By this I mean, when any non-master client player kicks the ball, each client experience the ball movement in a different way (like it is not smooth enough, for some it is quick enough and for some the ball lags while moving) even though the direction and force applied is same through RPC call. I know this may be because of different calculations done by different clients system. But I need to smooth it out and be efficient enough (ball needs to be moved with same velocity/force) for all clients connected. Looking for ways I can fix this issue. Any guidance appreciated. Really appreciate your time.

2

2 Answers

1
votes

It is recommended that ShootBallRPC calls be sent only to the master client, calculate and control the position of the ball on the master client, and then synchronize it to other clients in real time (by photonview).

0
votes

This worked for me.When player kick the ball then transfer the ownership

void OnCollisionEnter2D(Collision2D col) {

    if (PlayerPrefs.GetInt("OnlineMode") == 1)
    {
        if (col.gameObject.tag == "Player")
        {
            this.photonView.TransferOwnership(col.gameObject.GetComponent<PhotonView>().Owner);
        }
        
    }
}