I have written some code to generate some terrain. It uses perlin noise to decide what type of tile to spawn. I am happy with the results of using perlin noise, but there is a problem. I would assume that with this code a different terrain would be generated, because the perlin noise generated would be different. However this doesn’t work, so is there a problem in the code? Or is the perlin noise cached and I have to generate a new one at runtime? Here is my code:
using UnityEngine;
using System.Collections;
public class TerrainGeneration : MonoBehaviour {
public float tileWidth;
public GameObject grass;
public GameObject dirt;
public GameObject water;
private float[,] terrainArray;
// Use this for initialization
void Start () {
Vector2 currentPos = (Vector2)transform.position;
for (float x = 0; x < 100; x++) {
for (float y = 0; y < 100; y++) {
float noise = Mathf.PerlinNoise (x / 10f, y / 10f);
if (noise < 0.2) {
Instantiate (water, currentPos, transform.rotation);
} else if (noise > 0.2 && noise < 0.8) {
Instantiate (grass, currentPos, transform.rotation);
} else {
Instantiate (dirt, currentPos, transform.rotation);
}
currentPos = currentPos + new Vector2 (tileWidth, 0);
}
currentPos = new Vector2 (0, currentPos.y + tileWidth);
}
}
// Update is called once per frame
void Update () {
}
}