【问题标题】:Photon Unity3d: OnPhotonSerializeView() doesn't workPhoton Unity3d:OnPhotonSerializeView() 不起作用
【发布时间】:2019-10-12 11:50:44
【问题描述】:

我正在开发一款多人汽车游戏。 一切正常,主人和客户都很好地连接到房间。 (我一直在用马可波罗教程) 问题是当我看到其他汽车在屏幕上移动时,位置会通过传送汽车来更新。一直出现又消失。

我的部分代码:

PhotonNetwork.automaticallySyncScene = false;

public class CNPlayerManager : Photon.MonoBehaviour
{
...
 void FixedUpdate()
    {
        if (photonView.isMine)
        {
            //it works fine
        }
        else
        {
            transform.position= Vector3.Lerp(transform.position, this.correctPosition, Time.deltaTime * 5);
            transform.rotation= Quaternion.Lerp(transform.rotation, this.correctRotation, Time.deltaTime * 5);
        }
    }

public void OnPhotonSerializeView(PhotonStream stream, PhotonMessageInfo info)
{
    if (stream.isWriting)
    {
            stream.SendNext(transform.position);
            stream.SendNext(transform.rotation);
        
    }
    else
    {
            this.correctPosition = (Vector3)stream.ReceiveNext();               //Line 100
            this.correctRotation = (Quaternion)stream.ReceiveNext();       //Line 101
        
    }
}

我的汽车预制件中的 PhotonView 是这样的:
(来源:photonengine.com
但在我的光子版本中,我有更多选择。在所有者中,我有“在运行时设置”和“固定”。在“观察到的组件”中,我有 2 个组件,我的汽车预制件和脚本 CNPlayerManager。

当我玩两辆车时,在第一辆车上我有时会收到这个错误:“IndexOutOfRangeException: Array index is out of range. PhotonStream.ReceiveNext ()...”在第 100 行。 在第二辆车上我得到了同样的结果。

你能帮帮我吗?

【问题讨论】:

    标签: unity3d multiplayer photon


    【解决方案1】:

    当您说“观察汽车预制件”时,您是什么意思?我认为在这种情况下只观察 CNPlayerManager 脚本就足够了,因为它处理位置和旋转同步。

    不过,我建议您查看 PUN 包中已包含的脚本,尤其是 PhotonTransformView 脚本。这个可以完成您想要实现的目标,并且还提供了一些同步选项,您可以检查并查看哪一个最适合您的需求。

    如果您仍然遇到问题,请告诉我们。

    【讨论】:

      猜你喜欢
      • 2021-04-12
      • 2021-03-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-06-10
      • 1970-01-01
      • 2018-10-08
      • 1970-01-01
      相关资源
      最近更新 更多