- Home /
2d Top-Down Perlin noise map using Mathf.PerlinNoise
Okay I don't really like asking for help, but here I go: I've been trying to make a 2d top-down terrain generator for my game ( something like dwarf fortress, just... simpler), and I managed to make one (Hooray!), but the terrain my script generates isn't really what I was trying to do. I hope you get my idea, I want a more realistic, bumpy terrain. Here is my code:
public class Generator : MonoBehaviour {
public GameObject dirtPrefab;
private GameObject C;
private float maxX = 320;
private float maxY = 320;
private int seed;
void Start()
{
Regenerate();
}
private void Regenerate(){
float width = dirtPrefab.transform.lossyScale.x / 5;
float height = dirtPrefab.transform.lossyScale.y / 5;
for (float i = 0; i < maxX; i++)
{
for (float k = 0; k < maxY; k++)
{
var perlin = Mathf.PerlinNoise(i / 10, k / 10);
if (perlin >.5f)
{
C = (GameObject)Instantiate(dirtPrefab, new Vector3(i * width, k * height, 2), Quaternion.identity);
SpriteRenderer Sr1 = C.GetComponent<SpriteRenderer>();
Sr1.color = Color.green;
}
}
}
}
}
Any ideas on how I can get a better terrain generator?
,
Answer by nisovin · Apr 04, 2016 at 11:34 PM
First, you're using 0.5 as a cutoff for "land" vs "not land". But what you really want to do is use that as a height value. You can see an immediate difference by changing your color:
Sr1.color = new Color(0, perlin, 0);
This will make your land color change.
Second, you can try messing with your divisor when generating the Perlin noise. Right now you're using 10, but try using different numbers to see what the results are.
Third, you can try to offset the returned perlin value based on how far from the center of the map you are. Areas near the center would have a higher value, and areas farther away would have a lower value. This would kind of force the map to generate a mountain in the center with ocean around the edges.
Alright, I tried everything you suggested, except the mountain thing (It isn't really what I'm trying to achieve) Here is what i modified:
var perlin = $$anonymous$$athf.PerlinNoise(i / 50, k / 55); Sr1.color = new Color(0, perlin, 0);
Changing the divisor to generate the noise only makes the islands bigger, but their shape stays the same. Isn't there a way to make natural looking islands using $$anonymous$$athf.PerlinNoise? $$anonymous$$aybe some way to affect the frecuency of the noise used to créate the perlin noise?
Perlin noise is just noise, there are no options for it. It generates numbers that smoothly transition between 0 and 1, and that's it. You can use it as part of your process for generating an island, but you can't just use it directly. You should really try scaling by distance though, it'd probably help get what you're trying to achieve. But if you want something that really looks nice, it's gonna have to be more complicated.
Answer by Benster1599 · Aug 06, 2017 at 01:03 AM
I know this post is quite old, but does anyone know how I could make this into an infinite terrain? Thanks
Edit: Sorry. This should be in the comments section.
Old post I know but for anyone that stumbles across this Sebastian Lague on YouTube dose some A class tutorials on this subject they are truly the best i have found. Link to his playlist below for anyone that is interested.
This is also a little late to be answering, but the Perlin noise map is either infinite or close enough to infinite for practical purposes. You can use chunks, and whenever a player approaches the edge of a chunk grab another bit of the map and use it to generate terrain.
Old post I know but for anyone that stumbles across this Sebastian Lague on YouTube dose some A class tutorials on this subject they are truly the best i have found. Link to his playlist below for anyone that is interested.
Answer by JohnnyFactor · Sep 14, 2020 at 03:15 PM
Mix the perlin noise with a radial gradient to get an island in the center. I found an excellent blog post with various examples:
https://edwardtoy.wordpress.com/2014/01/16/final-year-project-lets-make-some-noise/