# Why is this generating flat terrain?

I’m using the following piece of code to generate psuedo-random terrain similar to that in Minecraft.

``````using UnityEngine;

/// <summary>
/// Terrain generator.
/// </summary>
/// <param name="cube">The cube to instaniate while generating terrain.</param>
/// <param name="noiseScale">The factor to multiply noise height by.</param>
public class TerrainGenerator : MonoBehaviour
{
public GameObject cube;
public int worldWidthX;
public int worldWidthZ;

/// <summary>
/// Generates the terrain.
/// </summary>
public void GenerateTerrain()
{
for(int x = 0; x <= this.worldWidthX; ++x)
{
for(int z = 0; z <= this.worldWidthZ; ++z)
{
float y = Mathf.PerlinNoise(x / 30, 76) * Mathf.PerlinNoise(z / 30, 22) * 40;
Instantiate(this.cube, new Vector3(x, y, z), this.cube.transform.rotation);
}
}
}

void Start()
{
this.GenerateTerrain();
}
}
``````

I’m using the algorithm provided in the answer to this question, but I still get a completely flat result, as seen below:

Can anyone tell my why I’m getting a result like this?

You’re using integer division; e.g. x/30 will always equal 0 until x is >= 30.

Change

``````float y = Mathf.PerlinNoise(x / 30, 76) * Mathf.PerlinNoise(z / 30, 22) * 40;
``````

to

``````float y = Mathf.PerlinNoise(x / 30.1f, 76) * Mathf.PerlinNoise(z / 30.1f, 22) * 40;
``````