UNet - sync child transform scale
How can I use the Unet framework to sync the localScale changes of a spawned GameObject's children??
In my game, my players meshes belong to a "PlayerModel" child of my "Player" gameobject. I want people to be able to customize their players by adjusting the scaling of the playermodel object before coming into the game but can't get it to sync right... It works well enough whenever I try to alter the "Player" object itself but that also alters things that I don't want altered. I've tried using SyncVar, I've tried using Rpcs and I feel like I've tried about everything else... Here's my code:
(also btw the data is stored on an external server - hence the "OnRecievedData" function and all that jazz... don't worry about any of that, it works fine")
public class PlayerCustoms : NetworkBehaviour {
[SyncVar]
[SerializeField]
GameObject playerModel;
AccountManager accountManager;
GameManager gameManager;
InGameData inGameData;
PlayerDataHandler dataHandler;
public string data = "";
Player[] players;
private NetworkIdentity objNetID;
bool dataRecieved = false;
[SyncVar]
public Vector3 scale = new Vector3 (1, 1, 1);
void Start(){
accountManager = AccountManager.GetInstance ();
gameManager = GameManager.GetInstance ();
dataHandler = PlayerDataHandler.GetInstance ();
inGameData = InGameData.GetInstance ();
SetUniqueData ();
}
public void SetUniqueData(){
dataRecieved = true;
accountManager.InvokeGetData (OnDataRecieved);
}
void OnDataRecieved(string data){
this.data = data;
dataRecieved = true;
inGameData.allCustomDimensions.Add (GetComponent<Player> ().username, DataTranslator.DataToDimensions (data));
print ("Data recieved: " + data);
print ("New dimensions will be " + DataTranslator.DataToDimensions (data));
dataRecieved = true;
Vector3 newScale = DataTranslator.DataToDimensions (data);
SetScale (newScale);
}
void SetScale(Vector3 scale){
if (isLocalPlayer) {
playerModel = transform.Find ("Graphics").transform.Find ("PlayerModel").gameObject;
print ("Found playerModel: " + playerModel);
CmdScale (playerModel, scale);
}
}
[Command]
void CmdScale(GameObject playerModel, Vector3 scale){
Debug.Log ("CMDSCALE");
objNetID = playerModel.GetComponent <NetworkIdentity> ();
//if (isServer) {
// objNetID.AssignClientAuthority (connectionToClient);
RpcScale (playerModel, scale);
// objNetID.RemoveClientAuthority (connectionToClient);
//}
}
[ClientRpc]
void RpcScale(GameObject playerModel, Vector3 scale){
Debug.Log ("RPCSCALE");
playerModel.transform.localScale = scale;
Debug.Log ("Set scale for " + playerModel + " to " + scale);
}
}
Answer by CStampGames · Feb 19, 2018 at 02:44 AM
solved it
public class PlayerCustoms : NetworkBehaviour {
[SerializeField]
Transform playerModel;
void Start(){
Player[] players = GameManager.GetInstance ().GetAllPlayers ();
foreach (Player player in players) {
player.GetComponent <PlayerCustoms> ().CustomizeLocal ();
}
}
[Client]
public void CustomizeLocal (){
if (!isLocalPlayer)
return;
Vector3 scale = DataTranslator.DataToDimensions (InGameData.GetInstance ().dataDic [AccountManager.GetInstance ().username]);
Color[] colors = DataTranslator.DataToColor (InGameData.GetInstance ().dataDic [AccountManager.GetInstance ().username]);
CmdOnCustomize (scale, colors);
}
[Command]
void CmdOnCustomize(Vector3 scale, Color[] colors){
RpcOnCustomize (scale, colors);
}
[ClientRpc]
void RpcOnCustomize(Vector3 scale, Color[] colors){
ApplyCustomization (scale, colors);
}
private void ApplyCustomization(Vector3 scale, Color[] colors){
playerModel.localScale = scale;
for (int i = 0; i < colors.Length; i++) {
playerModel.GetChild (i).GetComponent <MeshRenderer> ().material.color = colors [i];
playerModel.GetChild (i).GetComponent <MeshRenderer> ().material.SetColor ("_EmissionColor", colors [i]);
}
}
}