- Home /
make grid of gameobjects between 2 points
I have been trying to do this for a few hours in unity now but cant get my head around it. basically i have 4 points(vector3) 1 each for the 4 corners topleft, topright, bottomleft, bottomright. now i have a sprite that i use as a block what i want to do is basically fit X number of blocks within the four points. see the image below. I cant think of a way to do this right now. i would appreciate any Ideas on how to do this.
Answer by b1gry4n · Apr 07, 2016 at 11:58 PM
Wanted to make sure this works 100%, so I did a test run.
The Setup: If you want it to line up, offset the mesh/sprite object inside a parent game object by half so that the transform of the parent sits at the bottom left corner of the object. Notice where the transform is in the image
The Result:
using UnityEngine;
using System.Collections;
public class ResizeGrid : MonoBehaviour {
public Transform pointA;
public Transform pointB;
public Transform pointC;
public GameObject spritePfb;
public int gridWidth = 5;
public int gridHeight = 5;
void Start()
{
GenerateGrid(pointA.position, pointB.position, pointC.position);
}
void GenerateGrid(Vector3 a, Vector3 b, Vector3 c)
{
GameObject container = new GameObject();
container.name = "Grid Container";
container.transform.position = c;
float spriteWidth = Vector3.Distance(a, b) / gridWidth;
float spriteHeight = Vector3.Distance(a, c) / gridHeight;
for (int x = 0; x < gridWidth; x++)
{
for (int y = 0; y < gridHeight; y++)
{
GameObject sprite = (GameObject)Instantiate(spritePfb, container.transform.position, Quaternion.identity);
sprite.transform.parent = container.transform;
sprite.transform.localPosition = new Vector3(x * spriteWidth, y * spriteHeight, 0);
sprite.transform.localScale = new Vector3(spriteWidth, spriteHeight, 1);
}
}
}
}
Answer by Happy-Zomby · Apr 07, 2016 at 08:38 PM
Hi, What part are you struggling with? You will need to instantiate 5 rows of 5 sprites - to do so you use something like this
for (int i = 0; i < 5; i++)
{
for (int j = 0; j < 5; j++)
{
//here you put your code to instantiate 1 sprite
//then you use i and various items to set your Y position
//and j to set the X position
}
You will need to calculate your distance between top left and top right and between top left and bottom left and divide that by 5 then use that to scale your sprite and also to move in on x or y (multiplying it by i or j) depending... you may also need to add an additional half of that 5th if you want it to start with the top left corner in the top left of your first sprite.
Below is some code I used to generate a grid - it may help
function GenerateGrid ()
{
generateGrid = false;
for(var i : int = -halfWidthX; i <halfWidthX; i++)
{
for(var z : int = -halfHeightZ; z <halfHeightZ; z++)
{
var tempTile = Instantiate(tile,Vector3(startPos.x+i,startPos.y,startPos.z+z),Quaternion.identity);
tempTile.transform.name = "Tile";
tempTile.transform.parent = gridHolder;
}
}
}
hope that helps,
this part is good. i am struggling with scaling the sprite so that they fit next to each other.the way i am doing it is, I take 2 points - left and right. then generate the vectors in between the two points by using lerp in a nested loop to get the full set of vector3 positions for all the game objects. the only problem is, when i place the sprites in position they overlap each other.i need to scale them so that they fit within the 2 points edge to edge. so really the scaling is the only issue. any pointers on that?
Did you try using the local scale of your sprite? transform.localscale? assu$$anonymous$$g you initial block size is 1 x 1 you would change the localscale.x to 1/5 of the distance between your topleft and top right (e.g. if you distance is 8.5) and the distance between top and bottom is 12.
yourSprite.transform.localScale = new Vector2(8.5/5,12/5);
//or something like that
does that help?
In any case I believe the answer of @b1gry4n below answers to your needs and is much more complete than $$anonymous$$e
hey @happy Zombay. yup that answer helped.the resizing method is perfect after i tweaked it to maintain the square shape . will put up the final code i use over here in some time. thank you both for the help appreciate it. :)