2D world generation with perlin noise is producing regular terrain
Hello, I have a Perlin noise script on my 2d game that generates "random terrain" but so far it only generates perfect waves for the terrain.
If you only generate one it looks kind of good but when you start generating more the pattern starts being much more noticeable.
From what I've gathered, the script seems to choose a fixed vertical position and then draw a wave to it multiple times, but I have no idea what-s causing that.
Here's the script: using UnityEngine; using UnityEngine.Tilemaps;
public class proceduralgeneration : MonoBehaviour
{
[SerializeField] int width;
[SerializeField] int minStoneheight, maxStoneHeight;
[Range(0, 100)]
[SerializeField] float heightValue, smoothness;
[SerializeField] Tilemap grassTilemap, dirtTilemap, stoneTilemap;
[SerializeField] Tile grass, dirt, stone;
float seed;
void Start()
{
seed = Random.Range(-1000000, 1000000);
Generation();
}
private void Update()
{
if(Input.GetKeyDown(KeyCode.O))
{
seed = Random.Range(-1000000, 1000000);
Debug.Log(seed);
Generation();
} if(Input.GetKeyDown(KeyCode.P)) {
grassTilemap.ClearAllTiles();
dirtTilemap.ClearAllTiles();
stoneTilemap.ClearAllTiles();
}
}
void Generation()
{
for (int x = 0; x < width; x++)
{
int height = Mathf.RoundToInt(heightValue * Mathf.PerlinNoise(x / smoothness, seed));
int minStoneSpawnDistance = height - minStoneheight;
int maxStoneSpawnDistance = height - maxStoneHeight;
int totalStoneSpawnDistance = Random.Range(minStoneSpawnDistance, maxStoneSpawnDistance);
for (int y = 0; y < height; y++)
{
if (y < totalStoneSpawnDistance)
{
stoneTilemap.SetTile(new Vector3Int(x, y, 0), stone);
}
else
{
dirtTilemap.SetTile(new Vector3Int(x, y, 0), dirt);
}
}
if(totalStoneSpawnDistance == height)
{
stoneTilemap.SetTile(new Vector3Int(x, height, 0), stone);
}
else
{
grassTilemap.SetTile(new Vector3Int(x, height, 0), grass);
}
}
}
}
I also tried another kind of procedural generation, bu copying game objects and selecting a random value on where to put a tile (If next to, one block up or one block down), which never produced regular terrain but the curves and flat surfaces were choppy as there was random 1 block wide gaps and it wasn't optimized at all.
It would mean a lot if any of you could help me out, and as I'm starting with this as there could be something that might be off, thanks in advance :)
Your answer
![](https://koobas.hobune.stream/wayback/20220613011648im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Player follows mouse, but I need only Y 1 Answer
2D Rotation with Quaternion gets stuck... 1 Answer
How to lock the camera on Y axis? 0 Answers
Arkanoid-like bounce troubles 1 Answer