Photon Pun 2 用鼠标改变位置

Photon Pun 2 change position with mouse

我认为 Photon 和鼠标位置之间存在问题。因为当我尝试使用键盘更改对象的位置时,它可以成功运行,但是当我尝试使用鼠标更改位置时,它不会通过网络进行更改。如何在网络上使用鼠标位置更改对象位置?

public class SpehreControl : MonoBehaviour
{
    PhotonView PV;
    GameObject sphere1;
    GameObject bt;
    // Start is called before the first frame update
    void Start()
    {
        PV = GetComponent<PhotonView>();
        sphere1 = GameObject.Find("Sphere 1");
        bt = GameObject.Find("BT_1");
    }

    // Update is called once per frame
    void Update()
    {

            if (Input.GetMouseButton(0) && PV.IsMine)
            {
            PV.RPC("RPC_MoveOnline", RpcTarget.AllBuffered);
            }

    }



    [PunRPC]
    void RPC_MoveOnline()
    {
        transform.position = new Vector3(Camera.main.ScreenToWorldPoint(Input.mousePosition).x,
           Camera.main.ScreenToWorldPoint(Input.mousePosition).y, 0);

    }

}

我认为是RPC函数的问题。 当您调用它时,每个用户只会收到一个没有参数的调用事件。这意味着,每个用户都使用 他自己的 Input.mousePosition 而不是来自发件人。

您可以使用参数来解决这个问题:

...
   PV.RPC("RPC_MoveOnline", RpcTarget.AllBuffered, Camera.main.ScreenToWorldPoint(Input.mousePosition));
...
[PunRPC]
void RPC_MoveOnline(Vector3 worldPosition)
{
    transform.position = new Vector3(worldPosition.x, worldPosition.y, 0);
}

但确实不是很好的方法...

据我所知,您需要为所有用户同步一些对象的位置。 处理 PhotonView 组件(你的 GO 已有)要容易得多。只需将您的 GO Transform 组件拖放到 unity inpector 中的 PhotonView Observed Components 字段

使用这个,你本地 GO 的所有变化都会自动同步给所有玩家! more info here

祝你好运!)