Search Unity

[Solved] Parenting not seen by new client

Discussion in 'Multiplayer' started by mkandersson, Jun 10, 2016.

  1. mkandersson

    mkandersson

    Joined:
    Oct 5, 2012
    Posts:
    21
    Hello. I am currently throwing together a little UNet game and I'm having some problem with parenting/equipping a weapon in the players hand. I've searched the net but I havent been able to find an answer.

    The host sees its weapon parented correctly but the client will only see an unparented weapon where the host char is standing. The host and client however sees the client weapon parented just fine.

    1. I have a weapon with a Network Identity script on it with no boxes checked
    and the weapon is registered on the Network Manager under spawnable prefabs.

    2. In Attack Script on Start (if isLocalPlayer) I call a CmdEquipWeapon method.
    Code (CSharp):
    1. [Command]
    2. public void CmdEquipWeapon()
    3. {
    4.   GameObject weapon = (GameObject)Instantiate(testWeapon, Vector3.zero, Quaternion.identity);
    5.   NetworkServer.Spawn(weapon);
    6.  
    7.   //RpcSetWeaponParent(gameObject, weapon, right);
    8.   rightWeapon = weapon; // SyncVar
    9. }
    3. First I tried with a [ClientRpc] call but then tried using [SyncVar(Hook="ParentWeapon")] instead.
    Code (CSharp):
    1. [ClientRpc]
    2. void RpcSetWeaponParent(GameObject character, GameObject weapon, bool right)
    3. {
    4.     Transform hand = right ?
    5.         character.GetComponent<AttackBase>().rightHand.transform :
    6.         character.GetComponent<AttackBase>().leftHand.transform;
    7.  
    8.     Transform wt = weapon.transform;
    9.     wt.SetParent(hand);
    10.     wt.localPosition = Vector3.zero;
    11.     wt.localRotation = Quaternion.identity;
    12. }
    Code (CSharp):
    1. void ParentWeapon(GameObject weapon)
    2. {
    3.     Transform wt = weapon.transform;
    4.     wt.SetParent(rightHand.transform);
    5.     wt.localPosition = Vector3.zero;
    6.     wt.localRotation = Quaternion.identity;
    7. }

    EDIT: Never mind I managed to solve it. Read up on using hooks on SyncVars and found out it doesn't call the hook on init, so I forced an update on the SyncVar when a new client connects. This will call the hook method again and parent it correctly. Not the best method but will do for now.
     
    Last edited: Jun 13, 2016