【问题标题】:How can I change other clients variable in Unity Photon multiplayer?如何在 Unity Photon 多人游戏中更改其他客户端变量?
【发布时间】:2016-09-27 13:34:45
【问题描述】:

我对 PunRpc 调用有点困惑,我尝试了 PhotonTarget.Others 看看它是否会健康——;在其他客户端上,但它没有工作。

此刻我想知道我怎样才能做到健康——;当我按空格键时在其他客户端上。

这是我尝试过的: 这是命令:

photonView.RPC("healthReduction", PhotonTargets.Others, null);

这就是 RPC

    [PunRPC]
void healthReduction()
{
    health--;
    Debug.Log("Health--");
}

但还是不行。

【问题讨论】:

  • 你有播放器脚本的代码吗?
  • 您的日志中有任何内容吗?如果方法不存在或参数不匹配,PUN 会输出错误。您不记录 RPC 调用。你确定它会被调用吗?有关 RPC 使用示例,请参阅 PUN 演示。
  • 问题是通过 RPC 调用,我不能减少 ENEMYS 的生命值,我只能减少我的生命值。因此,当我调用 PhotonTargets.Others 时,它将在我的播放器上执行代码,但只有其他人可以看到我的播放器的更新健康状况,而我不能。我想在 ENEMY 的播放器上执行此代码,但现在我相信 RPC 调用不可能,只能更新您自己的变量,以便其他人可以看到它们,但要获取敌人的变量并修改它是不可能的我相信:/ .

标签: unity3d unity5 photon unity-networking


【解决方案1】:

我通过使用一个实例并使用 2 个不同的命名脚本解决了这个问题,例如,如果我想从 PlayerManager 访问 PlayerManager1 脚本,我这样做PlayerManager1.Instance.photonView.RPC("reduceMyHealth",PhotonTargets.All,null)

这将调用 PlayerManager1 中的 reduceMyHealth() PunRpc。

为此,您必须将此代码添加到您要访问的脚本中: static public PlayerManager1 Instance; 和 start(){}Instance = this;

【讨论】:

    【解决方案2】:

    我认为这很容易解决。 源代码如上。

    public class TestPhoton : Photon.PunBehaviour
    {
        public PhotonView gameView;
    
        void Start()
        {
            gameView = this.GetComponent<PhotonView>();
        }
    
        public void OnClickTest()
        {
            photonView.RPC("HealthReduction", PhotonTargets.Others);
        }
    
        [PunRPC]
        public void HealthReduction()
        {
            health--;
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-07-24
      • 2022-01-14
      • 2015-12-16
      • 2013-01-12
      • 1970-01-01
      相关资源
      最近更新 更多