- Home /
2D Game Visual Grid to Place Objects
Hi, im currently working on a 2D game where the player has an a few objects he can select and move around in the level. Now i want him to be able to move them while snapped to visible grid on screen. How do i go on about doing this?
I have some ideas about translating the objects around, but how do i make them snap to points? How do i create a visible grid on screen?
Thanks in advance Unity Community :)
Answer by Piflik · Jan 03, 2013 at 01:49 PM
You can compute the grid-positions with an integer division by the grid width. When the player lets go of the object you can then snap it to the nearest grid position:
Example:
Vector3 temp = transform.position;
temp.x = (int)(temp.x / gridWidth) * gridWidth;
temp.y = (int)(temp.y / gridHeight) * gridHeight;
temp.z = 0;
transform.position = temp;
The visible grid would probably be a simple GUITexture.
Thank You, i will give it a try and come back to you on it :)
Answer by ben.hartmann · Jan 10, 2013 at 08:46 PM
Hey,
did Philipps answer help you out? I'm trying to make a tile based grid to, if its realy just a Guitexture i lost some hours googling :)