- Home /
The question is answered, right answer was accepted
How can I create a grid for a Leveleditor?
Hello,
I want to make a grid, but the instantiated grid-prefabs are not aligning right next to each other, they are overlapping:
for(var x : int = 0; x < sizeX; x++){
for(var y : int = 0; y < sizeY; y++){
gridClone = Instantiate(grid, Vector3.zero, Quaternion.identity);
gridClone.transform.position = Vector3(x,0,y);
}
}
Looks like you need to take into account the size of the prefab. Try multiplying the x and y values of the position by about 16.
gridClone.transform.position = Vector3(x * 16,0,y * 16);
Answer by Noxury · Jul 23, 2015 at 08:14 PM
Nevermind, I have found a solution:
for(var x : int = 0; x < sizeX; x++){
for(var y : int = 0; y < sizeY; y++){
gridClone = Instantiate(grid, Vector3(x*24,0,y*24), Quaternion.identity);
}
}
}
Note that 24 is the width/height of the grid prefab.
Need to provide how much space is in between the instantiated objects. Can make public properties and adjust as needed:
public float spacingX = 10f;
public float spacingY = 10f;
then in the for-loop, multiple the position with the spacings:
gridClone.transform.position = Vector3(x * spacingX, 0f, y * spacingY);
Note that the variable "y" is actually using the Z position. I named the property |spacingY| to keep consistent with the existing code. It should be "z".
Follow this Question
Related Questions
Prefabs are instantiated in editor but not in executable 2 Answers
Why my prefab is auto changing? 3 Answers
Add prefabs to hierarchy without game running? 1 Answer
Instantiate in Editor 1 Answer