# Procedural terrain

I have been working on an procedural terrain, which eventually is going to be infinite.
I have run into a couple of problems. Some of which I can’t figure out how to fix.
The main issue right now is this: Dropbox - Error - Simplify your life
This is the current code:

``````function GenerateTile(nw : int, nh : int) {
if (GameObject.Find("Chunk "+nw+","+nh)) {
return;
}
var td = new TerrainData();
td.heightmapResolution = 513;
td.size = new Vector3(500,200,500);

var ChunkPosX : float = td.size.x * nw;
var ChunkPosY : float = td.size.z * nh;

var t = Terrain.CreateTerrainGameObject(td);
var hw : float = Terrain.activeTerrain.terrainData.heightmapWidth;
var hh : float = Terrain.activeTerrain.terrainData.heightmapHeight;
var heights = new float[hw, hh];
for (var x = 0; x < hw; x++) {
for (var y = 0; y < hh; y++) {
//           World Pos Divided by heightmap height
var xCoord = ((ChunkPosX + x) / (Mathf.PI * scale));
var yCoord = ((ChunkPosY + y) / (Mathf.PI * scale));
heights[x,y] += Mathf.PerlinNoise(xCoord, yCoord);
}
}
Terrain.activeTerrain.terrainData.SetHeights(0,0,heights);
t.transform.position = new Vector3(ChunkPosX, 0,ChunkPosY);
t.name = "Chunk "+nw+","+nh;
}
``````

your main problem seems to be that you use the terrain size to find the perlin start point however you should be using a multiple of your heightmap resolution, also I’m not quite certain why (I haven’t done much work with terrain heightmaps) but for some reason I had to swap the position they were mapped to in the float list:

``````function GenerateTile(nx : int, nz : int) {
if (GameObject.Find("Chunk "+nx+","+nz)) {
return;
}
var td = new TerrainData();
td.heightmapResolution = 513;
td.size = new Vector3(500,200,500);

var t = Terrain.CreateTerrainGameObject(td);
var hw : float = Terrain.activeTerrain.terrainData.heightmapWidth;

var ChunkPosX : float = td.size.x * nx;
var ChunkPosZ : float = td.size.z * nz;

var startPosX : int = hw*(nx-1);
var startPosZ : int = hw*(nz-1);

var heights = new float[hw, hw];
for (var x = 0; x < hw; x++) {
for (var z = 0; z < hw; z++) {
var xCoord = ((startPosX + x)/scale);
var zCoord = ((startPosZ + z)/scale);
heights[z, x] = Mathf.PerlinNoise(xCoord, zCoord);
}
}
Terrain.activeTerrain.terrainData.SetHeights(0,0,heights);
t.transform.position = new Vector3(ChunkPosX, 0,ChunkPosZ);
t.name = "Chunk "+nx+","+nz;
}
``````

Hope that helps!

Scribe