# Random Terrain Generation

Hello, i would like somebody help me.

I would like some help with random terrain generation.
My terrain is made out of cubes(Scale 1 x 1 x 1) and as i have it right now, it generates 10 cubes in each axis.
So the terrain is flat.

What i would like all of you to help me with is the random terrain generation, i want my cubes to be generated randomly(from a seed).
Code and explanation would be greatly appreciated.

The code i use currently is :

``````	for(int x = 0; x < 10; x++){
for(int y = 0; y > -10; y--){
for(int z = 0; z < 10; z++){
if(y == 0){
Instantiate (Grass,new Vector3(x,y,z),Quaternion.identity);

}
else{
if (y != -9){
Instantiate (Stone,new Vector3(x,y,z),Quaternion.identity);
}
}

if(y == -9){
Instantiate (Stoprock,new Vector3(x,y,z),Quaternion.identity);
}
}
}

}

}
``````

perlin noise

`````` for(int x = 0; x < 10; x++){
for(int y = 0; y > -10; y--){
int t = Random.Range(0,3);
if(t == 0){
Instantiate (Grass,new Vector3(x,y,0),Quaternion.identity);

}
if (t == 1){
Instantiate (Stone,new Vector3(x,y,0),Quaternion.identity);
}

if(t == 2){
Instantiate (Stoprock,new Vector3(x,y,0),Quaternion.identity);
}
}
}

}

}
``````

Untested but it should be pretty random

Could you explain the perlin noise ?
//
Jamster, i will try that

I suggest you take a look at this :

having a different GameObject for each cube will slow down the program, a lot. Try building meshes from many cubes.

As for Perlin Noise (or you can also use Simplex Noise), it is a function where you send X and Y (or Z if you prefer) position and it gives you a value that you can use for height. The 2d perlin noise function is included in the Mathf class. Thereâ€™s also a 3d version to make caves, you give it the whole vector3 and it gives you a density. From a certain density (you chose which is your threshold) you replace it with air, the rest are other types of blockâ€¦

Hope it helps you. Seems we are many to work on voxel (minecraft like) enginesâ€¦

Thanks, i surely will check it out.