- Home /
How can I create a grid of transforms?
I want to procedurally create a grid of transforms as large as I specify, I do know that I will be using a loop however I don't fully understand loops. I just need someone to write some psuedo code and give an explanation and I can go from there.
Answer by Seth-Bergman · Jul 20, 2012 at 05:32 AM
Edited code: This should be what you want.. you have to initialize prefab to the object you are creating.. Either by simply drag and drop in the inspector, or by scripting if necessary.
This could be done in the Start function:
function Start(){
prefab = GameObject.FindWithTag("grid object"); // for example
}
As for setting the name, probably not necessary, you can access them all via the array, as in:
my2DArray[x,y] (where x and y are the grid coordinates, from 0 to 9 in this case..)
you create a two-dimensional array:
var x = 10;
var y = 10;
var spacing : float = 10;
var prefab : GameObject;
var my2DArray : GameObject[,] = new GameObject[x,y];
function Start{
for(var i = 0;i < x;i++)
{
for(var j = 0;j < y;j++)
{
my2DArray[i,j] = Instantiate(prefab, Vector3(j * spacing,i * spacing,0), transform.rotation);
j++
}
i++
}
}
by nesting the loops this way, it goes through the whole thing hope this helps
EDIT:
to clarify-
for(var i = 0;i < x;i++)
the first part (var i = 0;) is the var you will use to increment through the loop
the second (i < x;) is the condition which,when no longer met, will end the loop
the third (i++;) is the incrementation of the var
so, we start with i, which is equal to 0. Each pass, we do i++ ( i + 1 ). When i is no longer less than x (which is 10), the loop ends (does not get entered if condition is not met)
once the inner loop completes a full cycle, it ends, causing the outer loop to increment by 1, then re-enter the inner loop.. if that makes sense
the your code here part is where I really need help but thanks for clearing the loops part up, now I just need to know how to instantiate several gameObjects, arrange them in a grid, and name them A1, B1, B2, ect.
I've been doing the same thing, both with for-loops, foreach-loops, and for-loops with arrays (exactly like your example), but each time it produces the max number of tiles, but it does it in a diagonal, stacking the rows in this diagonal! I'll post my code, but I know I'm years off here...
Answer by flamy · Jul 20, 2012 at 06:21 AM
// im assuming that you need to generate in xz plane...
var ObjectToSpawn : Transform;
function GenerateTransforms(xLength:int,zLength:int,startPosition:Vector3)
{
for(var i=0;i<xLength;i++) // switches to the next column of z, continues until xLength times it has switched.
{
for(var j=0;j<zLength;j++) // generates zLength number of transforms along z axis.
{
var temp:GameObject = (GameObject) Instantiate(ObjectToSpawn,
Vector3(startPosition.x+ObjectToSpawn.localScale.x*i/*+offset if any*/,
startPosition.y,
startPosition.z+ObjectToSpawn.localScale.y*j/*+offset if any*/),
ObjectToSpawnRotation);
//places the object according the start position.
}
}
}
Answer by elesser · Jul 21, 2012 at 07:35 AM
what we are trying to work out with the grid is an AI system, the idea is to set up a grid so that when the player clicks a point to move a unit to the computer can move it in a non stupid way. like say the unit is in D5 and the player clicks in A3 the unit would move and when it hit a trigger to change directions it looks at 2 arrays to know what way to go. in other words if it needed to turn left or right the arrays would say have letters and say left had A B C and right had E F G it would check see A in the 1st one and know to go left same for up and down 1 2 3 4 and the 2nd was 6 7 8 9 then it would check see 3 in the 1st one and know to go up. maybe not the best way to do something like this but its the idea i had
Your answer
Follow this Question
Related Questions
How to fill a volume with a procedurally generated 3d voxel grid? 0 Answers
How to generate a grid for procedurally generated platforms 1 Answer
Increase height of panel w/ gridlayout when new objects added 1 Answer
Problem with arrays and while loops 0 Answers
GridLayout dynamic cell size based on object count 0 Answers