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:
using UnityEngine;
using UnityEngine.Networking;
using System.Collections;
public class CharBuild : NetworkBehaviour
{
[SerializeField]
private Transform headJoint;
[SerializeField]
private Transform bodyJoint;
[SerializeField]
private Transform footJoint;
[SyncVar(hook = "ChangeHead")]
public int headIndex = -1;
[SyncVar(hook = "ChangeBody")]
public int bodyIndex = -1;
[SyncVar(hook = "ChangeFoot")]
public int footIndex = -1;
int[] parts;
void Start()
{
if(isLocalPlayer)
{
parts = GameController.Instance.charBuildSys.GetPartsIndex();
CmdSetParts(parts);
}
else
{
if(!isServer)
{
InstantiatePart(GameController.Instance.charactersParts.headParts[headIndex].gameObject, headJoint);
InstantiatePart(GameController.Instance.charactersParts.bodyParts[bodyIndex].gameObject, bodyJoint);
InstantiatePart(GameController.Instance.charactersParts.footParts[footIndex].gameObject, footJoint);
}
}
}
[Command]
void CmdSetParts(int[] partsSended)
{
headIndex = partsSended[0];
bodyIndex = partsSended[1];
footIndex = partsSended[2];
}
void ChangeHead(int headIndex)
{
Debug.Log("Instanciado por hook");
InstantiatePart(GameController.Instance.charactersParts.headParts[headIndex].gameObject, headJoint);
}
void ChangeBody(int bodyIndex)
{
InstantiatePart(GameController.Instance.charactersParts.bodyParts[bodyIndex].gameObject, bodyJoint);
}
void ChangeFoot(int footIndex)
{
InstantiatePart(GameController.Instance.charactersParts.footParts[footIndex].gameObject, footJoint);
}
void InstantiatePart(GameObject part, Transform joint)
{
part = (GameObject)Instantiate(part, Vector3.zero, Quaternion.identity);
part.transform.SetParent(joint, false);
part.transform.localPosition = new Vector3(0,0,0);
part.GetComponent<BodyPartAttributes>().isMine = isLocalPlayer;
}
}
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)