We are doing a similar thing in our multiplayer game. To do this, you need to do a few actions. Firstly, the concept of:
Installing a weapon parent on a server is trivial, as you already found. Just install the parent transformer, as usual in Unity. However, after this object appears on the server using NetworkServer.Spawn it will later be spawned on clients in the root of the scene (the hierarchy outside the generated collection is not synchronized).
So, to set up the hierarchy on the client, I would assume that you:
- Use SyncVar to synchronize the netID of the parent between the server and client.
- When the object is created on the client, find the parent using the synchronized network identifier and set it as the transformation parent.
So I would tweak your code to look something like this. First, set the parent netId object before you create it. This ensures that when it is created on clients, netId will be installed.
[Command] void Cmd_EquipWeapon() { var weaponObject = Instantiate (Resources.Load ("Gun"), hand.position, Quaternion.Euler (0f, 0f, 0f)) as GameObject; weaponObject.parentNetId = hand.netId;
And then in your weapon class:
- Add property parentNetId.
- Mark it as
[SyncVar] so that it synchronizes between the server and client copies. - When generating on the client, find the parent using netId and set it to the transform parent.
Perhaps something like:
[SyncVar] public NetworkInstanceId parentNetId; public override void OnStartClient() {
source share