Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I have a player object who can equip multiple weapons. When a weapon is equipped, its transform's parent is set to its hand. I have messed around with this for some time and cannot get this to work for both the host and the client. Right now I am trying to equip the weapon on the server, and tell all the clients to set their parents transforms.

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;
    }

And in the update I am updating the weapons transform

    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;
            }
        }
    }

I know this isn't the most optimized way to do this..but right now I am just trying to get this to work any way possible and then work on tweaking it. Seems like it should be a simple task.

like image 762
James Avatar asked Jul 11 '15 17:07

James


2 Answers

We do a similar thing in our multiplayer game. There are a few things you need to do to get this working. Firstly, the concept:

Setting the weapon's parent on the server is trivial, as you have found. Simply set the transform's parent as you would normally in Unity. However, after spawning this object on the server with NetworkServer.Spawn, it will later be spawned on clients in the root of the scene (hierarchy outside of the spawned prefab is not synchronised).

So in order to adjust the hierarchy on the client I would suggest that you:

  • Use a SyncVar to synchronise the netID of the parent object between the server and client.
  • When the object is spawned on the client, find the parent using the synchronised netID and set it as your transform's parent.

Therefore, I would adjust your code to look something like this. Firstly, set the weapon's parent netId before you spawn it. This will ensure that when it is spawned on clients, the netId will be set.

[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
}

And then in your weapon class:

  • Add a parentNetId property.
  • Mark it as [SyncVar] so that it synchronises between server and client copies.
  • When spawned on a client, find the parent object using the netId and set it to our transform's parent.

Perhaps something like:

[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);
}
like image 70
Andy Barnard Avatar answered Oct 04 '22 09:10

Andy Barnard


I found this post really useful but I have a small addendum.

The netId will be on the root of the hierarchy so its useful to know that you can traverse down the hierarchy using transform.Find.

Something like this ..

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

    transform.SetParent(parentObject.transform.Find(pathToWeaponHolder));
like image 37
JonJon Avatar answered Oct 04 '22 09:10

JonJon