I am new to procedural generation in Unity and I am making a simple game with a player in a plane that is procedurally generated, the requirement is that I am trying to bring up some walls simultaneously with the generation of the plane which is not working. The code so far for this is below:
public GameObject WorldPlane;
public GameObject Cube;
public GameObject walls;
private int radius = 5;
private int planeOffset = 10;
private Vector3 startPos = Vector3.zero;
private int XPlayerMove => (int)(Cube.transform.position.x - startPos.x);
private int ZPlayerMove => (int)(Cube.transform.position.z - startPos.z);
private int XPlayerLocation => (int)Mathf.Floor(Cube.transform.position.x / planeOffset) * planeOffset;
private int ZPlayerLocation => (int)Mathf.Floor(Cube.transform.position.z / planeOffset) * planeOffset;
Hashtable tilePlane = new Hashtable();
void Update()
{
generateWorld();
}
private void generateWorld()
{
if (startPos == Vector3.zero)
{
for (int x = -radius; x < radius; x++)
{
for (int z = -radius; z < radius; z++)
{
Vector3 pos = new Vector3((x * planeOffset + XPlayerLocation),
0,
(z * planeOffset + ZPlayerLocation));
if (!tilePlane.Contains(pos))
{
GameObject tile = Instantiate(WorldPlane, pos, Quaternion.identity);
tilePlane.Add(pos, tile);
}
}
}
}
if (hasPlayerMoved(XPlayerMove, ZPlayerMove))
{
for (int x = -radius; x < radius; x++)
{
for (int z = -radius; z < radius; z++)
{
Vector3 pos = new Vector3((x * planeOffset + XPlayerLocation),
0,
(z * planeOffset + ZPlayerLocation));
if (!tilePlane.Contains(pos))
{
GameObject Wall=Instantiate(walls, pos, Quaternion.identity);
GameObject tile = Instantiate(WorldPlane,pos,Quaternion.identity);
tilePlane.Add(pos, tile);
tilePlane.Add(pos, Wall);
}
}
}
}
}
private bool hasPlayerMoved(int playerX, int playerZ)
{
if (Mathf.Abs(XPlayerMove) >= planeOffset || Mathf.Abs(ZPlayerMove) >= planeOffset)
{
return true;
}
return false;
}
Here the cube is the player, the world plane is the prefab for the plane which should be generated, and the walls are the prefab for the game object that I am trying to instantiate with the plane, as of now the plane is getting generated without any issues. I really stuck with this, requesting help.