- Home /
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!
I assume you are talking about $$anonymous$$ecraft-style levels? Please give more detail about what you are trying to do. Is the level a fixed size, or do you want it to expand the farther you move along? Do you need the interior of objects for digging, or just the highest, visible level (a la Tactics games)?
I would like to know every possible way to create a generate a map using a script, but right now I want to know how to create a block based map with help of differnet parameters. If you know how to make a map that updates, you are welcome to tell me how to do one.
I would start to figure out what went wrong with what you were doing. There are so many ways to "randomly spawn cubes with instantiate" there is really no way to answer your question.
Answer by MOLB · Jun 02, 2012 at 09:41 AM
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.
Answer by Befall · Jun 01, 2012 at 02:31 PM
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.
Well, I would rather have a script that generates a map based on different parameters, like how long, wide and tall the map could be.
Oh, you wanted a script, not to do it yourself? Never$$anonymous$$d then.
I want to CREATE a script that uses different parameters to create the map, not to GET a script that someone else has done. $$anonymous$$y question was how I could create a block based map with a script, not to get a scripts from someone.
Answer by Bunny83 · Jun 01, 2012 at 04:58 PM
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.
I have used Unity for a long time, with some smaller and longer breaks. I know most of the stuff in Unity, but sometimes the thing I am trying to create suddenly stop working. Sometimes there are both advanced or basic things that I have never used (because I was never in need of it). I made map generators before, one of them working. I have also managed to loose the other map generator script. Why I am asking is that I want to find out if there are new and better ways to create what I want. Right now I wanted to know if there is a better command/way to spawn the blocks... The way I wrote it might sound like I am a beginner and I want to create a game in one click. But that is not the case. I know that it isn't drag and drop, I have made games and I am well aware of the work you have to put in to see a good result. I will edit the question now to make it easier to understand.
Answer by pborg · Jun 01, 2012 at 06:59 PM
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
Answer by Agelos · Nov 22, 2014 at 10:11 PM
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;
}
}
Your answer
Follow this Question
Related Questions
2D Dynamic map generation 1 Answer
Create 3D mesh from PNG 0 Answers
Merge 2D background with 3D Character 0 Answers
Why unity decrease audio quality? 2 Answers