简体   繁体   中英

Unity3D: Photon syncing physics events

I'm making a billiard game and I wanna implement multiplayer in it. Since updating the movement in every frame is inefficient and laggy, I thought I could remake the events in game happen in all clients but for some reason they differ from one another.

The direction that the ball launches is correct but it starts to differ for different users when it collides with other balls(if it collides only with the wall it's fine). Technically this shouldn't happen IMO.

This is the code I use to add force to the player ball:

I use this to launch the player ball

photonView.RPC("FireBall", PhotonTargets.All, storedDirection, power);

This is the function that adds the force to the player ball.

[PunRPC]
    public void FireBall(Vector3 stordDir , float poww)
    {
        rigidbody.AddForce(stordDir.normalized * poww, ForceMode.Impulse);
    }

I use this function to get the desired direction of the player.

public void UpdateStickPosition()
    {
        Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
        RaycastHit hit;
        if (Physics.Raycast(ray, out hit, Mathf.Infinity, layerMask))
        { 
            if (Input.GetKeyDown(KeyCode.Mouse0)  
            {
                storedDirection = hit.point - transform.position;
            }
        }

    }

I appreciate all help given, don't hesitate to ask me any questions.

Unitys物理引擎不是确定性的(即结果在所有计算机上并不总是相同的)-对象的实际位置可能不同步。

Instantiate the ball as a networked object using photons API. I think it should sync its properties across all the players if you do that, including any position/physics change.

Sample Code: "photonNetwork.Instantiate(PrefabName,transform.position,transform.rotation,0);"

Check out their manual. https://doc.photonengine.com/en-us/pun/current/getting-started/pun-intro

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM