- Home /
Perlin noise gives the same result every time
I generate random terrain like this:
void GenerateTerrain(float tileSize) {
float[,] heights = new float[terrain.terrainData.heightmapWidth, terrain.terrainData.heightmapHeight];
for (int i = 0; i < terrain.terrainData.heightmapWidth; i++) {
for (int k = 0; k < terrain.terrainData.heightmapHeight; k++) {
heights[i, k] = Mathf.PerlinNoise(((float)i / (float)terrain.terrainData.heightmapWidth) * tileSize, ((float)k / (float)terrain.terrainData.heightmapHeight) * tileSize) / 10.0f;
}
}
terrain.terrainData.SetHeights(0, 0, heights);
}
But for some reason the terrain never changes. No matter how many times I start the game, it's exactly the same. What can I do to fix this?
Answer by AlwaysSunny · Feb 09, 2015 at 02:27 PM
PerlinNoise IS always the same every time! (Which can be leveraged to your advantage; for instance, if you needed to reset all changes to the current world, just run the genny again with the same seed).
If you sample the same location with the same scaling, it's the same result each time. To get the randomness you're seeking, you need to inject some randomness into your parameters.
Each time you generate a new world, create a seed (can be an integer, but pick a big one). Use the seed when sampling PerlinNoise:
((i / heightmapWidth) * tileSize) + randomSeed
Also, unless I'm misremembering, I think your tileSize variable can be somewhat arbitrary, which will result in a different density of hills and valleys. It's been a while since I worked with that noise class, but I think that's the case.
Good luck,
Your answer
![](https://koobas.hobune.stream/wayback/20220613182819im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Difficulty with Perlin Noise based Terrain Generation 2 Answers
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
perlin noise terrain is spikey 0 Answers
How to handle Perlin Noise ? 0 Answers