unet synchroinze simple procedurally created map on clients
So, I need to create procedural map composed from different chunks, as you can see on screenshot they are platform chunks, and column chunks. I encountered dozens of problems.
My first idea was to create new gameobject in code to parent all column chunks and than parent it to selected platform, the problem is parenting is not transfered to clients. I also have no idea how to register procedurally generated on server object on client, I guess this is impossible, because you can only pass simple stuff like IDs and seeds etc.
So I tried to do all of this just with spawn for testing, to forget about parenting, regestration... And I encountered one more problem: NetworkServer.Spawn doesn't send localscale
This is how my current code works:
Gameobject with script LevelBuilder, which is NetworkBehaviour calls CmdGenerateLevel in Start.
[Command]
public void CmdGenerateLevel(int size, int angle)
{
_angle = angle;
_size = size;
for (var i = 0; i <= size; i++)
{
_platformLayers.Add(new List<GameObject>());
}
var initialPlatform = Instantiate(PlatformPrefab);
NetworkServer.Spawn(initialPlatform);
_platformLayers[0].Add(initialPlatform);
_offsetY = initialPlatform.transform.position.y;
GeneratePlatformsRecursive(initialPlatform, 0);
SpawnColumnsOnRandomPlatforms(10);
StartCoroutine(DestroyOverTime());
}
Constraints are:
All of the chunks can be damaged or destroyed during the game;
Clients can join during the game and must get the current state.
Question: Can you suggest me a good way of synchronizing this chunks between server and clients? And what is a good way to SetParent of object on both server and client?
It sounds like something easy, but I'm still newbie in networking, so even after 8 hours of trying to find a solution I still haven't managed to do it right.
Screenshot with how the game looks like on server and how I also want it to be seen on client.
Also code example for generation of this random state
private void BuildColumnOnPlatform(GameObject parentPlatform, GameObject columnsRoot)
{
var randomScale = Random.Range(-3.5f, +5f);
Vector3(column.transform.localScale.x, column.transform.localScale.y + Random.Range(-0.5f, +1f), column.transform.localScale.z);
var mainChunk = Instantiate(ColumnPrefab, parentPlatform.transform.position, parentPlatform.transform.rotation);
mainChunk.transform.position = new Vector3(mainChunk.transform.position.x, mainChunk.transform.position.y + (mainChunk.transform.localScale.y + randomScale) * 0.45f, mainChunk.transform.position.z);
mainChunk.transform.localScale = new Vector3(mainChunk.transform.localScale.x, mainChunk.transform.localScale.y + randomScale, mainChunk.transform.localScale.z);
NetworkServer.Spawn(mainChunk);
var decorCount = Random.Range(1, 4);
for (int i = 0; i < decorCount; i++)
{
var randomElevation = Random.Range(-1f, 0.5f);
var randomRotation = new Vector3(Random.Range(-5.5f, 5.5f), Random.Range(10, 40), Random.Range(-5.5f, 5.5f));
var decorChunk = Instantiate(ColumnPrefab, parentPlatform.transform.position, parentPlatform.transform.rotation);
decorChunk.transform.position = new Vector3(decorChunk.transform.position.x, decorChunk.transform.position.y + randomElevation + (decorChunk.transform.localScale.y + randomScale) * 0.45f, decorChunk.transform.position.z);
decorChunk.transform.rotation = Quaternion.Euler(randomRotation);
decorChunk.transform.localScale = new Vector3(decorChunk.transform.localScale.x + Random.Range(-0.4f, 0.2f), decorChunk.transform.localScale.y + randomScale, decorChunk.transform.localScale.z + Random.Range(-0.2f, 0.22f));
NetworkServer.Spawn(decorChunk);
}
}
So since I asked this question, I started developing awful(but I hope it will work) solution. With synclist of structures of scale values and object ids. And then setting it in ClientRPC when any client connects. Still wondering for good solution.
Your answer
Follow this Question
Related Questions
Unet NetworkServer.spawn, correcting my workflow: Found no behaviour for incoming Command 1 Answer
UNET 5.4 - Spawn an object from client...and move it around from client....? 1 Answer
NetworkServer.Spawn() only on Server (with registered prefabs) [Unity 5.2.3f1] 0 Answers
[UNET] Can't spawn players with code 1 Answer
Cannot Spawn networkbehaviour gameobject 0 Answers