Block based map generator

Hi

Here’s the deal, I want to use the Instantiate command to spawn cubes in the X Y Z axes. For example: width 100, lenght 100, height 100… The result could be that it spawns cubes 100x100x100 or 1000x1000x1000 if I would have smaller blocks. I have used the Instantiate command before to create cubes according to my parameters. But now it isn’t working. So I want to ask the community if you have a better way to spawn cubes like that. All answers are can be helpful!

For a recent university project, I did something similar, which was dynamic block-based 3D generation of planets. It was similar to Minecraft in that it used a “seed” for randomization, which meant that although things were randomly generated with different seeds, they were consistent within each game, as it maintained the same seed.

It just takes a bit of math and creativity, but you need to take a few things into account:

  • How do you want to store information on changed “chunks”?
  • How do you want to dynamically add and remove objects from the world as you move around?
  • How complicated do you want created parts of the world to be?

These things, though important, can be answered down the line as well. If you want somewhere to start, I’d say look into seed-based randomization/generation, which works in both 2D and 3D.

You might take a look at the MinePackage (Minecraft starter package). It’s a fully functioning minecraft-style map generator / renderer. It’s a very very basic version not much optimisations but you should get a clue how it works. If not that might be a bit too heavy for you. It’s not something like moving a cube from A to B.

I’ve answered this at least 10 times on this page. Those questions, this one included, are by far to general. A whole voxel-chunk-based terrain engine require a bit more than a simple script attached to a gameobject.

Unity is a very easy-to-use engine, however advanced topics stay advanced. There is no “game-prefab” you drop in your scene and your done.

Mainly what you want to do is build a nestedd for loop(a for loop within a for loop) running as a square giving you a grid just build the for loop so that it uses the counters of the for loop as its placement coordinates. Build variables called rendered_grid_x and render_grid_z so that it can be referenced whenever the player comes to close to the edge of the map.

You will likely also want to come up with themes depending how you plan to use the blocks. In an outdoor game with varying areas, you’ll need to build variables that decide which blocks are okay in which theme. In which case, if some blocks are suitable for multiple environments, you’ll need to have an array or another abstract data type. If you’re using water, you may want to have sections of maps or at least small portions prebuilt for sake of a fluid looking stream or lake rather than chunks of water in a meadow.

If anybody else has a better idea for the water system that would very likely help

I solved the problem with the old map generator. I had to do some other things in the script to make it work. Now it is working very well.

Heres is mine code is not voxel,call it blocks on C# script

using UnityEngine;
using System.Collections;

public class Blocks : MonoBehaviour
{

//Public variable for the size of the terrain, width and heigth
public Vector2 Size = new Vector2( 20 , 20 );

//Height multiplies the final noise output
public float Height = 10.0f;

//This divides the noise frequency
public float NoiseSize = 10.0f;

private GameObject root;


void OnGUI ()
{
	
	//Make a button that generates when you press it
	if(GUI.Button( new Rect( 10, 10, 100, 30 ), "Generate" ))
	{
		
		//Generate!
		Generate();

	}
	
}

//Function that inputs the position and spits out a float value based on the perlin noise
public float PerlinNoise(float x, float y)
{
	//Generate a value from the given position, position is divided to make the noise more frequent.
	float noise = Mathf.PerlinNoise( x / NoiseSize, y / NoiseSize );
	
	//Return the noise value
	return noise * Height;
	
}

//Call this function to generate the terrain
void Generate ()
{
	
	//If we find a gameobject called terrain, there's a high
	//chance that we have the previous terrain still there
	//So, let's delete it
	Destroy(GameObject.Find("Terrain"));

	
	//Create a new empty gameobject that will store all the objects spawned for extra neatness
	root = new GameObject("Terrain");
	
	//Put the root object at the center of the boxes
	root.transform.position = new Vector3( Size.x/2, 0, Size.y/2 );
	
	//For loop for x-axis
	for(int i = 0; i <= Size.x; i++)
	{
		
		//For loop for z-axis
		for(int p = 0; p <= Size.y; p++)
		{
			
			GameObject box = GameObject.CreatePrimitive(PrimitiveType.Cube);
			
			box.transform.position = new Vector3( i, PerlinNoise( i, p ), p);
			box.transform.parent = root.transform;
			
		}
		
	}
				
	//Move the root at the origin.
	root.transform.position = Vector3.zero;

}

}