Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice
  4. Dismiss Notice

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

Discussion in 'Scripting' started by barafranc44, Aug 4, 2018.

  1. barafranc44

    barafranc44

    Joined:
    Jul 14, 2017
    Posts:
    1
    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 : https://ibb.co/bQUeae

    [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.


    Code (CSharp):
    1. BuildingScript  
    2.  
    3. [SerializeField]
    4. private MousePositionChecker mousePositionChecker;
    5.  
    6. private Transform currentBuilding;
    7. private bool hasPlaced;
    8.  
    9. // Update is called once per frame
    10. void LateUpdate () {
    11.    if (currentBuilding != null && !hasPlaced)
    12.    {
    13.        //Vector3 m = Input.mousePosition;
    14.        //Vector3 p = Camera.main.ScreenToWorldPoint(m);
    15.  
    16.        //currentBuilding.position = new Vector3(p.x, p.y, 0);
    17.  
    18.        if(mousePositionChecker.mousePos)
    19.            currentBuilding.position = mousePositionChecker.mousePos.position;
    20.  
    21.        if (Input.GetMouseButtonUp(0))
    22.        {
    23.            hasPlaced = true;
    24.        }
    25.    }
    26. }
    27.  
    28. public void SetItem(GameObject b)
    29. {
    30.    hasPlaced = false;
    31.    currentBuilding = ((GameObject)Instantiate(b)).transform;
    32. }
    33.  
    34. MousePositionScript
    35. public Transform mousePos;
    36.  
    37. // Update is called once per frame
    38. void Update () {
    39.  
    40.    Vector3 pos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
    41.    RaycastHit2D hit = Physics2D.Raycast(pos, Vector2.zero);
    42.    if (hit != null && hit.collider != null && hit.transform.tag=="BuildArea")
    43.    {
    44.        mousePos = hit.transform;
    45.    }
    46.  
    47. }
     

    Attached Files:

    • Map.png
      Map.png
      File size:
      22.5 KB
      Views:
      1,095
    Last edited: Aug 4, 2018
  2. GroZZleR

    GroZZleR

    Joined:
    Feb 1, 2015
    Posts:
    3,201
    Snapping to a grid is easy:
    Code (csharp):
    1.  
    2. int x = Mathf.Round(Input.mousePosition.x / gridWidth) * gridWidth;
    3. int y = Mathf.Round(Input.mousePosition.y / gridHeight) * gridHeight;
    4.  
    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.
     
  3. Arno1975

    Arno1975

    Joined:
    Aug 14, 2013
    Posts:
    43
    how does your MousePositionChecker script look like

    using the same script and want it in a grid..can't figure it out
     
    Last edited: Sep 20, 2018
  4. matthewjd24

    matthewjd24

    Joined:
    Nov 18, 2013
    Posts:
    8
    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: https://forum.unity.com/threads/tutorial-making-a-grid-with-a-2d-array.626041/

    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.
     
    Last edited: Oct 28, 2019
  5. tamara_m

    tamara_m

    Joined:
    Aug 22, 2019
    Posts:
    1
    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!