I want to make an automated level generator for my multiplayer lan game and that’s works al fine. But when I also want to build the level on the clients the RPC function only get called on the server. I have tried a lot of things but I don’t know what I do wrong.
So the problem is the PRC only gets called on the server and not on the clients.
(and I know that Unet will be outdated but know a little bit how to use it and don’t want to learn another networking solution)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Networking;
public class LevelGeneration : NetworkBehaviour
{
public GameObject plank;
public GameObject building;
public GameObject buildingBackground;
private GameObject start;
private GameObject end;
[SyncVar]
public Vector2 range;
private Vector3 spawnPos;
private float lastYPos;
[SyncVar]
private float randomPosY;
[SyncVar]
private float randomValueChooseHouse;
[SyncVar]
private float randomValueChooseBackgroundHouse;
private bool tikTak;
// Start is called before the first frame update
void Start()
{
start = GameObject.FindGameObjectWithTag("Start");
end = GameObject.FindGameObjectWithTag("End");
if(isServer)
{
BuildMap();
}
}
[ClientRpc]
public void RpcPlaceBuildingOnClient(int i)
{
if (isServer)
{
return;
}
GameObject buildingObj = Instantiate(building, transform);
buildingObj.transform.position = spawnPos;
if (i == 0)
{
buildingObj.transform.position = new Vector3(buildingObj.transform.position.x, spawnPos.y - 2, buildingObj.transform.position.z);
}
else
if (i == 9)
{
end.transform.position = buildingObj.transform.position;
}
}
[ClientRpc]
public void RpcPlacePlankOnClient(int i)
{
if (isServer)
{
return;
}
GameObject plankObj = Instantiate(plank, transform);
plankObj.transform.position = spawnPos;
if (i == 0)
{
plankObj.transform.position = new Vector3(plankObj.transform.position.x, spawnPos.y - 2, plankObj.transform.position.z);
}
else
if (i == 9)
{
end.transform.position = plankObj.transform.position;
}
}
public void BuildMap()
{
spawnPos = start.transform.position;
for (int i = 0; i < 10; i++)
{
randomValueChooseHouse = Random.value;
if (randomValueChooseHouse >= 0.7)
{
//spawn Building and set on good location
GameObject buildingObj = Instantiate(building, transform);
buildingObj.transform.position = spawnPos;
RpcPlaceBuildingOnClient(i);
//doe iets als dit de eerste plank is en doe iets als dit laaste plank is
if (i == 0)
{
buildingObj.transform.position = new Vector3(buildingObj.transform.position.x, spawnPos.y - 2, buildingObj.transform.position.z);
if (isServer)
{
randomPosY = randomPosY - 2;
}
}
else
if (i == 9)
{
end.transform.position = buildingObj.transform.position;
}
}
else
{
if (isServer)
{
randomValueChooseBackgroundHouse = Random.value;
}
//spawn plank and set on good location
GameObject plankObj = Instantiate(plank, transform);
plankObj.transform.position = spawnPos;
RpcPlacePlankOnClient(i);
//doe iets als dit de eerste plank is en doe iets als dit laaste plank is
if (i == 0)
{
plankObj.transform.position = new Vector3(plankObj.transform.position.x, spawnPos.y - 2, plankObj.transform.position.z);
if (isServer)
{
randomPosY = randomPosY - 2;
}
}
else
if (i == 9)
{
end.transform.position = plankObj.transform.position;
}
if (randomValueChooseBackgroundHouse > 0.5)
{
GameObject buildBackgroundObj = Instantiate(buildingBackground, transform);
if (tikTak)
{
buildBackgroundObj.transform.position = new Vector3(plankObj.transform.position.x + Random.Range(-6, 6), plankObj.transform.position.y + Random.Range(2, 15), plankObj.transform.position.z + 100);
tikTak = false;
}
else
{
buildBackgroundObj.transform.position = new Vector3(plankObj.transform.position.x + Random.Range(-6, 6), plankObj.transform.position.y + Random.Range(2, 15), plankObj.transform.position.z);
tikTak = true;
}
}
}
//zet spawn pos op een bepaalde plek
lastYPos = randomPosY;
if (isServer)
{
randomPosY = Random.Range(range.x, range.y);
while (Mathf.Abs(lastYPos - randomPosY) > 2 && randomPosY >= lastYPos)
{
Debug.Log("hoi");
randomPosY = Random.Range(range.x, range.y);
}
}
spawnPos = new Vector3(spawnPos.x + 25, randomPosY, spawnPos.z);
}
}
}