- Home /
How to generate infinite cube world / minecraft terrain with perlin noise?
NOTE: This is NOT Me asking you to give me the code as that would be completely unfair to you and to me, In the sense that i would not be learning anything at all. Or me trying to make a Minecraft / Cube World clone. I am 15 years old and have been using Unity3D for a very long time now, I have become very advanced in Unity's interface and have taken up Javascript to forward my knowledge in Game development. For a small project i would like to learn first off, How to use perlin noise to generate Minecraft like terrain that is infinite in all directions except down, Down it should be 10 blocks (It's more of a exploration game then a mining game like minecraft and others) Once that has been achieved (And i know it wont be easy) i would like to also learn how to add different bioms Example: basic and most common is the wasteland, small pools of acid, tiny town like structures that will be made of prefabs i designed or just cube placeholders. I have many more things to ask but this is the most important and difficult wall i have to overcome. Thank you for your time in anwsering my question, Any feedback would be appreciated :) (Sorry if i misspelled some things my first language is not english)
Use Google. This has been asked and answered quite a lot.
The reason i asked on here is because i couldint get an anwser off of google from weeks of trying :P
Answer by brandonsay95 · Oct 10, 2013 at 07:54 PM
to generate a 16X16 chunk i use
for (float px = 0; px < width; px ++) { for (float py = 0; py< width; py ++) {
System.Random rnd = new System.Random();
var Perlin1 = Mathf.PerlinNoise((px+ transform.position.x) /a, b);
var Perlin2 = Mathf.PerlinNoise((py+ transform.position.z) /a, b);
createCube(new Vector3(px + transform.position.x
, Mathf.FloorToInt(Perlin1*40*Perlin2),
py + transform.position.z));
}
}
I have updated the chunk generator now you can set a render distance allowing you to render new chunks prier to you heading to the edge of a chunk
using UnityEngine; using System.Collections;
public class ChunkMap : MonoBehaviour { public Transform Chunk; public Transform player; int chunksize = 16; int render_distance = 6;
// Use this for initialization
void Start () {
GenerateChunks();
}
void GenerateChunks()
{
Vector3[] chunks = new Vector3[render_distance*render_distance];
bool[] chunksbool = new bool[render_distance*render_distance];
int i = 0;
for(int x=(render_distance/2)*-1;x<render_distance/2;x++)
{
for(int z=(render_distance/2)*-1;z<render_distance/2;z++)
{
int px = Mathf.RoundToInt(player.transform.position.x /16) * 16;
int pz = Mathf.RoundToInt(player.transform.position.z /16) * 16;
px = px+(x*16);
pz = pz+(z*16);
chunks[i] = new Vector3(px,0,pz);
chunksbool[i] = true;
i=i+1;
}
}
for(i=0;i<render_distance*render_distance;i++)
{
foreach (Transform child in transform)
{
if(child.transform.position == chunks[i])
{
chunksbool[i]=false;
}
}
}
for(i=0;i<render_distance*render_distance;i++)
{
if(chunksbool[i])
{
CreateChunk(chunks[i]);
}
}
}
void CreateChunk(Vector3 pos)
{
Transform chunk = Instantiate(Chunk,pos,Quaternion.identity) as Transform;
chunk.transform.parent = transform;
}
void DestroyChunks()
{
int px = Mathf.RoundToInt(player.position.x/16) * 16;
int pz = Mathf.RoundToInt(player.position.z/16) * 16;
int renderer = Mathf.RoundToInt(render_distance + (render_distance/2));
foreach (Transform child in transform)
{
Vector2 ch = new Vector2(child.transform.position.x,child.transform.position.z);
Vector2 pl = new Vector2(player.transform.position.x,player.transform.position.z);
float dist = Vector2.Distance(ch,pl);
if(dist>renderer*20)
{
Destroy(child.gameObject);
}
}
}
// Update is called once per frame
void Update () {
GenerateChunks();
DestroyChunks();
}
}
Thanks for the help brandon! If i need any other help i will be sure to pm you! :)