【问题标题】:Photon Pun 2 change position with mousePhoton Pun 2 用鼠标改变位置
【发布时间】:2020-04-18 18:23:06
【问题描述】:

我认为 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);

    }

}

【问题讨论】:

    标签: c# unity3d networking multiplayer photon


    【解决方案1】:

    我认为是 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 组件拖放到统一检查器中的 PhotonView Observed Components 字段。

    使用此功能,您的本地 GO 转换的所有更改将自动同步给所有玩家! more info here

    祝你好运!)

    【讨论】:

    • 我很乐意提供帮助)
    • 我能再问一个问题吗?我怎样才能像你所说的那样发送动态数组,比如函数的参数与rpc?这是我的数组public int[] selectedDom,我在像这样selectedDom = new int[28]; for (int i = 0; i &lt; 28; i++) selectedDom[i] = 28; 的启动函数中定义它,然后我尝试像这样private void RPC_Start(int[] selectedDomP) 发送它。但这就像你所说的代码一样工作。
    猜你喜欢
    • 2015-06-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-12-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-27
    相关资源
    最近更新 更多