Hi everyone,
I have a script that creates a grid of squares after being given a width and height and how many rows and columns I want for it. Works great to make rectangular grids, but I want to be able to fill an area with an arbitrary shape determined, ideally, by a surrounding object.
My code works well if I instantiate the objects inside a rectangular tube, but if the tube was , for example, circular, I have no idea how this would work.
This is the code I have at the moment:
public float areaWidth;
public float areaHeight;
public int numberOfBlocksX;
public int numberOfBlocksY;
public float factor;
private Vector3 blockScale;
private GameObject tempBlock;
void Start () {
int i=0;
int j=0;
//Time to create all the blocks we need.
while (i<(numberOfBlocksX*numberOfBlocksY)) {
GameObject block = Instantiate (blockType) as GameObject;
block.name = "block"+i;
block.transform.localScale = blockScale*factor;
i++;
}
//Now let's position those blocks on a grid, row by row
for (i=0; i<numberOfBlocksY; i++) {
for (j=0; j<numberOfBlocksX; j++){
tempBlock=GameObject.Find("block"+((j*numberOfBlocksY)+i));//This is how to traverse my self made "matrix".
Vector3 tempPos;
tempPos.x=(blockScale.x/2)+(blockScale.x*j)-(areaWidth/2);
tempPos.y=(blockScale.y/2)+(blockScale.y*i)-(areaHeight/2);
tempPos.z=0.0f;
tempBlock.transform.position = tempPos;
}
}
}
I believe I would have to do quite the rewrite to this to make it approximate other shapes, as this structure is very much a rectangular matrix however way you look at it, so any help appreciated.
I also realize I am kind of making my own matrix structure, perhaps there’s an easier way to make this, but I just couldn’t figure out how to make a matrix in C# in Unity in the way I wanted, because my idea was to have a fixed NxN matrix which I would use as a template on how to instantiate the rectangles (using 0s where none and 1s where an object goes), I just had no idea how to approach that or if it was a better solution. I would still not know how to detect the surrounding shape to approximate to.
Thanks in advance for any info!