# how do I tile this generated terrain infinitely around the player?

i made a script that instantiates cubes with for loops and perlin noise. is there any way i can tile this infinitely around the player? here is the script for the generation of one tile

public GameObject grass;
public int mapSizeX = 50;
public int mapSizeZ = 50;

public float heightScale = 20f;
public float detailScale = 20f;

float seed;
// Use this for initialization
void Start()
{
//if (instance == null)
//   instance = this;
for (int x = 0; x < mapSizeX; x++)
{
for (int z = 0; z < mapSizeZ; z++)
{
int y = (int)(Mathf.PerlinNoise((x + seed) / detailScale, (z + seed) / detailScale) * heightScale);

GameObject g = Instantiate(grass) as GameObject;
g.transform.position = new Vector3(x, y , z);
g.transform.SetParent(this.transform);
}
}
}


## 3 Answers

Well, you could instantiate an infinite number of cubes, but that would require an infinite amount of time and consume an infinite amount of memory. So this is not going to work. You have to set a range limit around the player.

But what if the player moves towards the end of that range?

In that case you simply use your map generation function again to spawn new cubes in the newly discovered areas and destroy those in the areas left behind.

In theory you need to check the player location each frame and max render distance you’d set for yourself, if players location is bigger then your render radius, you add tiles based on he’s axis he moves at and delete those that are out of range

You have to keep your map somewhere (Array, file, class,...) Then you create a tile grid that contain a fixed number of tile. This grid could be a 2 dimensions array. Then each time the player move you update your tile grid with the actual tile the player should see. If you have a 10x10 tile grid = 100 tiles you select the 100 tiles around the players. You pick the needed tile in your map Container and add them to the tile grid. Then you render only the tile from the tile grid. If between 2 render cycle the player don't move you don't need to update the grid.

• If the map is spread out "infiinitely" as described in the question, then we can't keep the whole thing anywhere: an infinite array or infinite file would require infinite memory or disc space. ;) – DMGregory Feb 24 '19 at 14:58
• i just want to make a chunk in front of the player when the player is reaching an edge of the chunk and when it is on the other chunk remove the last chunk – Indiepixel Feb 24 '19 at 22:44