I am developing a top down game with a randomly generated world made out of blocks.
I am using mathf.perlinnoise to get height for the terrain.
My problem is the user can remove or place blocks any time, meaning when they dig into the terrain I need blocks to be below this. But spawning in that amount of blocks, even with a terrain height of only 5 or 10 drops me to less than 3fps on my reasonably powerful PC when I have a 100x100 (x, z) world.
I would use occlusion culling however there is also NPC’s which the player controls who might be doing something off screen, meaning I need the blocks spawned in.
So my question is what is the best way of going about this so the world can be expand forever but keeping a reasonable FPS?
Gameobject count: Around 72,000
My generation Code:
public void GenTerrain() {
for (float x = 0.0f; x < 5; x += 0.05f) {
for (float z = 0.0f; z < 5; z += 0.05f) {
float y = (Mathf.PerlinNoise(x, z));
y = Mathf.Round(y * 10f) / 10f;
//Debug.Log(x + "" + y + "" + z);
//gameObjNo++;
GameObject newGo = (GameObject)Instantiate(block, new Vector3(x * 10 * 2, y * 10, z * 10 * 2), Quaternion.identity);
for (int i = -1; i < y * 10; i++) {
GameObject newGo2 = (GameObject)Instantiate(block, new Vector3(x * 10 * 2, i, z * 10 * 2), Quaternion.identity);
//gameObjNo++;
}
}
}
//Debug.Log("Game Object Count: " + gameObjNo);
}