- Home /
[C#] Wondering what is amiss with my 1D Perlin Noise Terrain Generator?
Hello,
To start off I should say that I am still very much a beginner at coding in general, so I must apologize if my incompetence is facepalm-inducing :p
As more or less a learning experience, I've set myself a goal of creating a simple C# script that generates randomized but realistic terrain, consisting of tiled prefabs. To me Perlin Noise seemed to be the way to go considering its relative simplicity (?) and the Mathf.PerlinNoise method.
The code I wrote below works and all, but I'm almost certain that it's just creating random height values rather than the smooth Perlin digits I ventured out for!
using UnityEngine;
using System.Collections;
public class terrainGenScript : MonoBehaviour
{
public GameObject block;
public int height;
public int width;
void RandWorldGen()
{
for (int x = -width; x < width; x++)
{
float thisRand = Mathf.Floor(Mathf.PerlinNoise(Random.value * x * 100, Random.value * x * 100) * Random.Range(-height, height));
Instantiate(block, new Vector3(x, thisRand, 0), Quaternion.identity);
for (float y = thisRand - 1; y >= -height; y--)
{
Instantiate(block, new Vector3(x, y, 0), Quaternion.identity);
}
}
}
void Update()
{
if (Input.GetMouseButtonDown(0))
{
GameObject[] gos = GameObject.FindGameObjectsWithTag("Respawn");
foreach (GameObject go in gos)
{
Destroy(go);
}
RandWorldGen();
}
}
}
It would be super amazing if one of you could help me out or point me in the direction that this script should be going :)
Thanks so much for reading!
Answer by villevli · Sep 17, 2016 at 08:14 AM
Don't use any randoms inside the loop because it will just make the noise random and not perlin. Also the value you multiply x by (scale) must be much lower. I made a fixed version of the script and added more parameters and OnValidate so you can just press play and tweak the parameters in the inspector and see results in realtime.
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class TerrainGenerator2D : MonoBehaviour
{
public GameObject block;
public int height = 10;
public int width = 40;
public float scale = 5f;
public float offset = 10;
public float seed;
private List<GameObject> blocks;
// Use this for initialization
void Start()
{
seed = Random.value * 100;
blocks = new List<GameObject>();
RandWorldGen();
}
void RandWorldGen()
{
for (int x = -width; x < width; x++) {
float thisRand = Mathf.Floor((Mathf.PerlinNoise(x * scale * 0.01f + offset, seed) - 0.5f) * height * 2);
blocks.Add(Instantiate(block, new Vector3(x, thisRand, 0), Quaternion.identity) as GameObject);
for (float y = thisRand - 1; y >= -height; y--) {
blocks.Add(Instantiate(block, new Vector3(x, y, 0), Quaternion.identity) as GameObject);
}
}
}
// Update is called once per frame
void Update()
{
if (Input.GetMouseButtonDown(0)) {
foreach (GameObject go in blocks) {
Destroy(go);
}
blocks.Clear();
RandWorldGen();
}
}
void OnValidate()
{
if (!Application.isPlaying || blocks == null) return;
foreach (GameObject go in blocks) {
Destroy(go);
}
blocks.Clear();
RandWorldGen();
}
}
That makes a lot of sense, thanks for clearing that up :) Also thanks for the script edit - I'll check it out tomorrow when I have access to a PC
Yep, looking awesome! Thanks again for the help
Answer by AurimasBlazulionis · Sep 17, 2016 at 05:50 AM
You are multiplying Perlin noise data with random number. Sure, that is why you get random results. Your line should look like this: float thisRand = Mathf.Floor((Mathf.PerlinNoise(Random.value * x * 100, Random.value * x * 100) - 0.5f) * height * 2);
Perlin noise generates data from 0 to 1, so I see you want range from -height to height. You have to subtract 0.5 from the generated data and then multiply by 2 times the height.
Second. Tiles are really expensive. Yes, it is quite hard to create a single mesh, but you will have to do something about that in order to make it really playable (though, separate tiles in 2D are much less expensive than in 3D world, because there are much less of them).
Lastly, you declare thisRand every time. Better store it as a private variable. The problem is that it does GC allocations which degrades performance especially on mobile.
Hello, and thanks! Trying out your recommendation, I certainly see what you mean, however it seems to yield results that are more or less the same. Like this:
Rather than something smoother such as this:
Or am I missing something here and the produced shape is Perlin, but something in its parameters are creating more jagged ground?
Also, I hadn't considered the mesh idea so I might look into that.
Thanks again
Your answer
Follow this Question
Related Questions
Distribute terrain in zones 3 Answers
2D Efficient Realtime Terrain Generation 0 Answers
2D Terrain 1 Answer
Procedural Island Terrain Generation 2 Answers
[C#] How to use a Multi Threaded Job Queue for Math function 2 Answers