Search Unity

Sync instantiated objects over Network (UNET)

Discussion in 'UNet' started by iagoccampos, Aug 19, 2015.

  1. iagoccampos

    iagoccampos

    Joined:
    Aug 2, 2014
    Posts:
    10
    Hello guys.

    I'm making a game that you can build you player, changing the head, torso and foot. After build the player you have the option to host a game or find a local. Suppose that I cliked to host one, the base player game object is instantiated and this base player game object have only joints and a script called CharBuild. This script will get the index of the body parts that the player choose and instantiate each part in the correct joint. Everything looks fine on both of sides(server/client), but I think that I done it in the wrongest and horrible way possible.

    Take a look:

    Code (CSharp):
    1.  
    2. using UnityEngine;
    3. using UnityEngine.Networking;
    4. using System.Collections;
    5. public class CharBuild : NetworkBehaviour
    6. {
    7.      [SerializeField]
    8.      private Transform headJoint;
    9.      [SerializeField]
    10.      private Transform bodyJoint;
    11.      [SerializeField]
    12.      private Transform footJoint;
    13.      [SyncVar(hook = "ChangeHead")]
    14.      public int headIndex = -1;
    15.      [SyncVar(hook = "ChangeBody")]
    16.      public int bodyIndex = -1;
    17.      [SyncVar(hook = "ChangeFoot")]
    18.      public int footIndex = -1;
    19.      int[] parts;
    20.      void Start()
    21.      {
    22.          if(isLocalPlayer)
    23.          {
    24.              parts = GameController.Instance.charBuildSys.GetPartsIndex();
    25.              CmdSetParts(parts);
    26.          }
    27.          else
    28.          {
    29.              if(!isServer)
    30.              {
    31.                  InstantiatePart(GameController.Instance.charactersParts.headParts[headIndex].gameObject, headJoint);
    32.                  InstantiatePart(GameController.Instance.charactersParts.bodyParts[bodyIndex].gameObject, bodyJoint);
    33.                  InstantiatePart(GameController.Instance.charactersParts.footParts[footIndex].gameObject, footJoint);
    34.              }
    35.          }
    36.      }
    37.      [Command]
    38.      void CmdSetParts(int[] partsSended)
    39.      {
    40.          headIndex = partsSended[0];
    41.          bodyIndex = partsSended[1];
    42.          footIndex = partsSended[2];
    43.      }
    44.      void ChangeHead(int headIndex)
    45.      {
    46.          Debug.Log("Instanciado por hook");
    47.          InstantiatePart(GameController.Instance.charactersParts.headParts[headIndex].gameObject, headJoint);
    48.      }
    49.      void ChangeBody(int bodyIndex)
    50.      {
    51.          InstantiatePart(GameController.Instance.charactersParts.bodyParts[bodyIndex].gameObject, bodyJoint);
    52.      }
    53.      void ChangeFoot(int footIndex)
    54.      {
    55.          InstantiatePart(GameController.Instance.charactersParts.footParts[footIndex].gameObject, footJoint);
    56.      }
    57.      void InstantiatePart(GameObject part, Transform joint)
    58.      {
    59.          part = (GameObject)Instantiate(part, Vector3.zero, Quaternion.identity);
    60.          part.transform.SetParent(joint, false);
    61.          part.transform.localPosition = new Vector3(0,0,0);
    62.          part.GetComponent<BodyPartAttributes>().isMine = isLocalPlayer;
    63.      }
    64. }
    I need to know if there is a way to do it better than it looks or do. Many thanks, Iago. (sorry my bad english)
     
  2. SuperNeon

    SuperNeon

    Joined:
    Mar 16, 2014
    Posts:
    85
    You should send the part index before to spawn the player.
    In this way it seems the replicated player will spawn with wrong parts before to receive it depending of ping.
    When the client request to be created (AddPlayer message) you can add a step to request the info of the player and spawn it properly.

    For the hook it is ok for me :)