Unity Multiplayer客户端未正确设置生成对象的父级

时间:2016-05-22 19:32:02

标签: c# unity3d multiplayer

所以我产生了玩家而不是他们拥有的枪支,在主机上它完美地工作但是在客户端枪支不会成为对象的实例。这是代码

 [Command]
 void CmdSpawn() {
     gun = (GameObject)Instantiate(gunToEquip, weaponPosition.position, weaponPosition.rotation);

     NetworkServer.SpawnWithClientAuthority(gun, connectionToClient);
     gun.transform.parent = weaponPosition;
 }

2 个答案:

答案 0 :(得分:1)

首先,您不是在客户端上设置父级。仅限在服务器上。主机同时是服务器和客户端,因此它可以工作。第二 - 你把枪预制件拖到网络管理员的可预制的预制插槽中吗?

另外,我相信你不应该这样做:

gun.transform.parent = weaponPosition;

请改用:

gun.transform.SetParent(weaponPosition);

试试:

[Command]
void CmdSpawn() 
{
    gun=(GameObject)Instantiate(gunToEquip,weaponPosition.position,weaponPosition.rotation);
    gun.transform.SetParent(weaponPosition);
    RpcSpawn();
}

[ClientRpc]
void RpcSpawn()
{
    if(NetworkServer.active) return;
    gun = (GameObject)Instantiate(gunToEquip, weaponPosition.position, weaponPosition.rotation);
    gun.transform.SetParent(weaponPosition);
}

答案 1 :(得分:1)

它似乎是Unity 5.1 Networking - Spawn an object as a child for the host and all clients

的副本

讨论的想法是同步父对象的网络ID,例如,在产生时在服务器端:

var gun = Instantiate (...);
gun.parentNetId = this.netId;
NetworkServer.Spawn (gun);

后来客户:

// GunController.cs
[SyncVar]
public NetworkInstanceId parentNetId;

public override void OnStartClient()
{
    GameObject parentObject = ClientScene.FindLocalObject(parentNetId);
    transform.SetParent(parentObject.transform);
}