Unity Grid Based Building (Snap to Grid) And Follow Mouse While Snapping

I am trying to build a 2d strategy game where you can build on a grid based world and all buildings will have different sizes like 3x3, 2x3 and they will be builded on the grid, based on their sizes. Without making them snap to grid depending on their size, i can make them just follow the mouse and build wherever i want but i need to make them snap to grid depending on their size. How can i achieve this ? Alternate image link : Map hosted at ImgBB — ImgBB

[EDIT] : Here is the final code down below. Right now i can snap and follow the selected building based on the mouse’s position with sending a ray and checking the building grids but still this action does not depend on the building’s sizes. Here is a video of current situation.

BuildingScript  

[SerializeField]
private MousePositionChecker mousePositionChecker;

private Transform currentBuilding;
private bool hasPlaced;

// Update is called once per frame
void LateUpdate () {
   if (currentBuilding != null && !hasPlaced)
   {
       //Vector3 m = Input.mousePosition;
       //Vector3 p = Camera.main.ScreenToWorldPoint(m);

       //currentBuilding.position = new Vector3(p.x, p.y, 0);

       if(mousePositionChecker.mousePos)
           currentBuilding.position = mousePositionChecker.mousePos.position;

       if (Input.GetMouseButtonUp(0))
       {
           hasPlaced = true;
       }
   }
}

public void SetItem(GameObject b)
{
   hasPlaced = false;
   currentBuilding = ((GameObject)Instantiate(b)).transform;
}

MousePositionScript
public Transform mousePos;

// Update is called once per frame
void Update () {

   Vector3 pos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
   RaycastHit2D hit = Physics2D.Raycast(pos, Vector2.zero);
   if (hit != null && hit.collider != null && hit.transform.tag=="BuildArea")
   {
       mousePos = hit.transform;
   }

}

Snapping to a grid is easy:

int x = Mathf.Round(Input.mousePosition.x / gridWidth) * gridWidth;
int y = Mathf.Round(Input.mousePosition.y / gridHeight) * gridHeight;

As for placing pieces, you’ll need to check the grid surrounding your snapped position. Pretty easy assuming you’re using a 1D or 2D array to represent your grid.

how does your MousePositionChecker script look like

using the same script and want it in a grid…can’t figure it out

Hi GroZZleR, I’m setting up a similar scenario and I was wondering what you meant about using a 1D or 2D array to represent the grid. The player will be able to place buildings on an isometric grid in the game, and it’s simple enough to determine to which grid he clicked on, but I’m trying to figure out how I will go about marking tiles as ‘occupied’/storing data on the positions of all his buildings, if that makes sense. I figure this is the purpose of the array you mentioned. Thanks!

Edit, I’ve found this: [Tutorial] Making a Grid with a 2D Array

Only issue is figuring out if this can be used with Unity’s tilemap, and if so, how that works, but I’m still working through the video. Unity’s tilemap is relatively new so there’s a lot of tutorial videos that don’t mention it.

If anyone is still interested in this topic, here is a video on how to make a 2d Grid Building System with standard Unity tilemaps. It is really easy, and can be used for any grid (isometric and regular). No need to write a custom grid for this!

1 Like