6

我有一个可以装备多种武器的玩家对象。装备武器时,其变换的父级设置为它的手。我已经搞砸了一段时间,无法让它对主机和客户端都有效。现在我正在尝试在服务器上装备武器,并告诉所有客户端设置他们的父母变换。

public NetworkInstanceId weaponNetId; 

   [Command]
    void Cmd_EquipWeapon()
    {
        var weaponObject = Instantiate (Resources.Load ("Gun"),
                                        hand.position,
                                        Quaternion.Euler (0f, 0f, 0f)) as GameObject;

        weaponObject.transform.parent = hand;

        NetworkServer.Spawn (weaponObject);

        //set equipped weapon
        var weapon = weaponObject.GetComponent<Weapon> () as Weapon;

        weaponNetId = weaponObject.GetComponent<NetworkIdentity> ().netId;
        Rpc_SetParentGameobject (weaponNetId);
    }

    [ClientRpc]
    public void Rpc_SetParentGameobject(NetworkInstanceId netID)
    {   
        weaponNetId = netId;
    }

在更新中,我正在更新武器变换

    void Update () {

    // set child weapon tranform on clients
    if (!isServer) {
        if (weaponNetId.Value != 0 && !armed) {
            GameObject child = NetworkServer.FindLocalObject (weaponNetId);


            if (child != null) {
                child.transform.parent = hand;
            }
        }
    }

我知道这不是最优化的方法。但现在我只是想让它以任何可能的方式工作,然后努力调整它。似乎这应该是一个简单的任务。

4

3 回答 3

16

我们在多人游戏中做类似的事情。您需要做一些事情才能使其正常工作。首先,概念:

如您所见,在服务器上设置武器的父级是微不足道的。只需像在 Unity 中一样设置变换的父级。但是,在服务器上使用 生成此对象后NetworkServer.Spawn,稍后将在场景根目录中的客户端上生成该对象(生成的预制件之外的层次结构不同步)。

因此,为了调整客户端的层次结构,我建议您:

  • 使用 SyncVar 在服务器和客户端之间同步父对象的 netID。
  • 当对象在客户端生成时,使用同步的 netID 找到父对象并将其设置为转换的父对象。

因此,我会调整您的代码,使其看起来像这样。首先,在生成武器之前设置武器的父 netId。这将确保当它在客户端上生成时,将设置 netId。

[Command]
void Cmd_EquipWeapon()
{
    var weaponObject = Instantiate (Resources.Load ("Gun"),
                                    hand.position,
                                    Quaternion.Euler (0f, 0f, 0f)) as GameObject;
    weaponObject.parentNetId = hand.netId; // Set the parent network ID
    weaponObject.transform.parent = hand; // Set the parent transform on the server

    NetworkServer.Spawn (weaponObject); // Spawn the object
}

然后在你的武器类中:

  • 添加 parentNetId 属性。
  • 将其标记为[SyncVar]使其在服务器和客户端副本之间同步。
  • 在客户端上生成时,使用 netId 找到父对象并将其设置为我们转换的父对象。

也许是这样的:

[SyncVar]
public NetworkInstanceId parentNetId;

public override void OnStartClient()
{
    // When we are spawned on the client,
    // find the parent object using its ID,
    // and set it to be our transform's parent.
    GameObject parentObject = ClientScene.FindLocalObject(parentNetId);
    transform.SetParent(parentObject.transform);
}
于 2015-07-14T21:04:51.307 回答
5

我发现这篇文章非常有用,但我有一个小附录。

netId 将位于层次结构的根部,因此知道您可以使用 transform.Find 遍历层次结构很有用。

像这样的东西..

GameObject parentObject = ClientScene.FindLocalObject(parentNetId);
    string pathToWeaponHolder = "Obj/targetObj";

    transform.SetParent(parentObject.transform.Find(pathToWeaponHolder));
于 2016-09-15T19:27:50.660 回答
1

在阅读了 Andy Barnard 的解决方案后,我想出了这个稍作修改的解决方案。代替 SyncVar,有一个客户端 RPC 供服务器在NetworkIdentity需要更改父级时调用。这确实要求父母也有一个NetworkIdentity(尽管它不需要是注册的预制件)。

public void Server_SetParent (NetworkIdentity parentNetworkIdentity) {
    if (parentNetworkIdentity != null) {
        // Set locally on server
        transform.SetParent (parentNetworkIdentity.transform);
        // Set remotely on clients
        RpcClient_SetParent (parentNetworkIdentity.netId, resetTransform);
    }
    else {
        // Set locally on server
        transform.SetParent (null);
        // Set remotely on clients
        RpcClient_SetParent (NetworkInstanceId.Invalid, resetTransform);
    }
}

[ClientRpc]
void RpcClient_SetParent (NetworkInstanceId newParentNetId) {
    Transform parentTransform = null;
    if (newParentNetId != NetworkInstanceId.Invalid) {
        // Find the parent by netid and set self as child
        var parentGobj = ClientScene.FindLocalObject (newParentNetId);
        if (parentGobj != null) {
            parentTransform = parentGobj.transform;
        }
        else {
            Debug.LogWarningFormat ("{0} Could not find NetworkIdentity '{1}'.", gameObject.name, newParentNetId.Value);
        }
    }
    transform.SetParent (parentTransform);
}

这两个是一个的一部分NetworkBehavior,这很明显RequiresComponent(typeof(NetworkIdentity))。如果您真的需要在客户端到服务器上的这种行为,我建议创建一个将 传递NetworkIdentity给服务器的命令,该命令只会调用服务器的公共方法。这是一组比最佳网络消息更理想的网络消息,但是嗯。

于 2017-10-18T16:01:04.733 回答