【问题标题】:Unity3D why my client can't call cmd method?Unity3D为什么我的客户端不能调用cmd方法?
【发布时间】:2019-01-13 16:29:28
【问题描述】:

我的问题是:我有一个包含武器对象的玩家,我的武器对象包含以下脚本:

private void FixedUpdate()
{
    AutomaticFirearmShoot();
}


public void AutomaticFirearmShoot()
{
    if (Input.GetButton("Fire1") && Time.time > _nextFire)
    {
        _nextFire = Time.time + _fireRate;      
        _player.GetComponent<NetworkShootManager>().CmdShoot();

    }

}

我的根播放器也包含这个脚本:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Networking;

public class NetworkShootManager : NetworkBehaviour
{
private void Update()
{
    if (!isLocalPlayer)
    {
        return;
    }
}

[Command]
public void CmdShoot()
{

   Debug.Log("shooting");

}


}

所以当我构建游戏时,只有宿主玩家可以调用CmdShoot方法。

另外,我的播放器有网络身份组件。

【问题讨论】:

    标签: c# unity3d networking


    【解决方案1】:

    您应该可以使用 Networking.NetworkBehaviour.isServer 来执行此操作(这已过时)。

    实现可能是这样的:

    [Command]
    public void CmdShoot()
    {
    
       if(!isServer)
           return; // If this is not being executed by the hoster then exit the method call.
    
       Debug.Log("shooting");
    
    }
    

    希望这会有所帮助!

    【讨论】:

    • 您可以发布一个明确的答案。您希望只有宿主才能访问 CmdShoot 命令还是所有人?
    • 我想要每个人。
    • 尝试从Update()中删除if(!isLocalPlayer)条件
    【解决方案2】:

    UNet 使用Remote Actions 在服务器和客户端上执行调用。 在您的情况下,您使用的是[Command],它在服务器上执行调用。 你将不得不使用。 [ClientRpc] 拜访客户。但请记住,客户端不能在不通过服务器的情况下调用其他客户端。所以你必须使用[Command] 方法,然后调用[ClientRpc]。

    此外,Unitys 网络管理器组件,让主机既成为客户端又成为服务器,因此主机将能够看到所有服务器调用等,而其他客户端则看不到。

    【讨论】:

      猜你喜欢
      • 2017-01-28
      • 2012-09-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-12-09
      • 2013-07-09
      • 1970-01-01
      相关资源
      最近更新 更多