- Home /
How to make a 40x40 map out of cubes, using script
I want to make a map out of single cubes (not just one big cube where i change the scale) This is because i want to make an interactive map.
Someone told me i could do this with script and prefabs to make the map.
Now my question is, how do you do this? I can make the prefabs my own, but i'm for sure not experienced enough to make my own script, although i'm still learning and by every script i examine, it gets me further in understanding the scripts.
NOTE : Im using Javascript.
I used this script first but the problem with this one is that it will just generate random objects in a certain Min and Max coordinate i had given. this brings me problems like : holes in the floor and several objects that are standing in the same coordinates
var myCube : GameObject;
function Start()
{
// Let's store some minimum and maximum possible x,y and z values
// to position our cubes:
var minX : int = -20;
var maxX : int = 20;
var minY : int = 1;
var maxY : int = 1;
var minZ : int = -20;
var maxZ : int = 20;
// (you can fiddle with these numbers to change the range of possible spawn positions)
var totalCubes : int = 500; // Change this to whatever number of cubes you'd like to have on-screen
var rot : Quaternion = Quaternion.identity;
for(i=0; i<totalCubes; i++)
{
// Use Random.Range to grab randomized x,y and z values within our min/max ranges:
var randomX : int = Random.Range(minX, maxX);
var randomY : int = Random.Range(minY, maxY);
var randomZ : int = Random.Range(minZ, maxZ);
var pos : Vector3 = new Vector3(randomX, randomY, randomZ);
Instantiate(myCube, pos, rot); // Put a new cube on the screen using a randomized position
}
}
function Update()
{
}
Then i used this script but it would just take to long to set all the blocks, and because i wanted to make a map out of 40x40 cubes that means i had to set 1600 blocks manually.
var myCube : GameObject;
function Start()
{
// Store a bunch of different positions in an Array:
var aPositions : Array = [new Vector3(0,1,0),
new Vector3(1,1,0),
new Vector3(2,1,0),
new Vector3(3,1,0),
new Vector3(4,1,0)];
var rot : Quaternion = Quaternion.identity;
// Loop as many times as there are elements in the aPositions Array:
for(i=0; i<aPositions.length; i++)
{
Instantiate(myCube, aPositions[i], rot); // Put a new cube on the screen using the i'th position
}
}
I can use every kind of help :) - Hexer.
Answer by AlucardJay · Jul 20, 2013 at 11:36 AM
You almost had the right idea in the first script. Let's look at the requirements :
a 2D grid of cubes
every position in the grid must have a cube
the height is different for each position in the grid
The way would be to loop through every x position, and within that every z position. Please read all the comments in my example script :
var myCube : GameObject;
var minX : int = -20;
var maxX : int = 20;
var minY : int = 0;
var maxY : int = 4;
var minZ : int = -20;
var maxZ : int = 20;
function Start()
{
// loop for every z position in the grid
for ( var z : int = minZ; z < maxZ; z ++ )
{
// now loop for every x position in the grid
for ( var x : int = minX; x < maxX; x ++ )
{
// get a random value for Y
var randomY : int = Random.Range( minY, maxY );
// put it all to together to assign a position
var pos : Vector3 = new Vector3( x, randomY, z );
// instantiate the cube into a variable, so you can do other things with it
var clone : GameObject = Instantiate( myCube, pos, Quaternion.identity );
// change the name of the object, include the x and z position in the name
clone.name = "Cube_" + x.ToString() + "_" + z.ToString();
// in future,
// this would be stored in a 2D array for future reference
// so you could modify the position, material, anything!
// cubeArray[x,z] = clone;
}
}
}
Thanks, but can i just replace.
// get a random value for Y
var randomY : int = Random.Range( $$anonymous$$Y, maxY );
// put it all to together to assign a position
var pos : Vector3 = new Vector3( x, randomY, z );
with
// loop for every y position in the grid
for ( var y : int = $$anonymous$$Y; y < maxY; y ++ )
// put it all to together to assign a position
var pos : Vector3 = new Vector3( x, y, z );
and change the $$anonymous$$Y and maxY to both 1, to get one layer ins$$anonymous$$d of two? I tried it out and it seems to work, but when i add gravity to my cube and click on play it seems to lagg very badly.
(I took my prefabs(cube) that was connected with the script and added a rigedbody by clicking on Component>Physics>Rigedbody)
In the future i want to make a cube fall when interacting with a player. (example : If a player walks over the cube, the cube starts to fall down. which affect just that one cube and not the cubes around it, unless the player walks of those cubes too.)
I can imagine it would lagg alot, or is this just a bug in unity that won't appear in the final stage?
Also how can i change the Y coordinate position of the cube? it doesn't seems to work when i change the $$anonymous$$Y and maxY to both 1.
i'm just new in scripting so i have no idea what a 2D array is :) do you got a link or an example so that i could understand what it is? Is is something that i have to implement in the script? and if so, where do i need to put it.
Your answer
![](https://koobas.hobune.stream/wayback/20220613110503im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Instantiate prefab within parameters?(Javascript) 1 Answer
I want to turn my flashlight on and off, while still maintaining the flicker effects. 3 Answers
Unity fire1 prefab in code 2 Answers
Too many bugs 0 Answers