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

Snap Objects into a grid

Discussion in 'Scripting' started by filipetakanap, May 30, 2022.

  1. filipetakanap

    filipetakanap

    Joined:
    Nov 16, 2021
    Posts:
    35
    Hello!

    I've tried everything but with no sucess. When you move the objects (link bellow) they should snap together or either snap into a grid in the plane.

    https://photos.app.goo.gl/1EU6K2sh7cQLACp6A

    I've tried fixed joint, collision detection, rigidbody freeze position, raycast but so far to no avail.

    All i have to move the objects on the plane is

    Code (CSharp):
    1.  void OnMouseDrag()
    2.     {
    3.         float distance_to_screen = Camera.main.WorldToScreenPoint(gameObject.transform.position).z;
    4.         Vector3 pos_move = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, distance_to_screen));
    5.         transform.position = new Vector3(pos_move.x, transform.position.y, pos_move.z);
    Thanks in advance
     
  2. Qriva

    Qriva

    Joined:
    Jun 30, 2019
    Posts:
    1,123
    You need to precise what do you mean by snap to grid. Do you mean scene grid? or runtime grid? or maybe freezing rotation on world axis?
     
  3. filipetakanap

    filipetakanap

    Joined:
    Nov 16, 2021
    Posts:
    35
    let me try to explain . By using the plane with a set value of "squares" that when i move the objects they stay right on that "squares". Like... aligned and neat, and not crashing into each other.
    Not sure if it is possible with unity...
     
  4. Qriva

    Qriva

    Joined:
    Jun 30, 2019
    Posts:
    1,123
    Everything is possible if you try hard enough :p

    If you want to take into account other objects and irregular shapes then it will require more work, but to do simple snapping you need to find nearest "cell" of grid, something like this:
    Mathf.RoundToInt(positonX / gridSizeX) * gridSizeX;
     
  5. filipetakanap

    filipetakanap

    Joined:
    Nov 16, 2021
    Posts:
    35
    I found out that there is a grid component that i can add to the terrain. is that it?

    EDIT: I've tried:
    Code (CSharp):
    1. float distance_to_screen = Camera.main.WorldToScreenPoint(gameObject.transform.position).z;
    2.         Vector3 pos_move = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, distance_to_screen));
    3.         pos_move.x = (pos_move.x / terrainGrid.cellSize.x) * terrainGrid.cellSize.x;
    4.         pos_move.y = (pos_move.y / terrainGrid.cellSize.y) * terrainGrid.cellSize.y;
    5.         transform.position = new Vector3(pos_move.x, transform.position.y, pos_move.z);
    but it does the same thing lol.
     
    Last edited: May 30, 2022
  6. RadRedPanda

    RadRedPanda

    Joined:
    May 9, 2018
    Posts:
    1,596
    Did you try any of the guides online? It seems like a common enough topic that there would be plenty of tutorials on how to do this stuff.
     
    Qriva likes this.
  7. Qriva

    Qriva

    Joined:
    Jun 30, 2019
    Posts:
    1,123
    I have no clue what is terrain grid.
    Well, it does because you did
    x / y * y
    and according to my math knowledge it still equals x :)
    Compare yours to what I posted.
     
  8. filipetakanap

    filipetakanap

    Joined:
    Nov 16, 2021
    Posts:
    35
    maybe i just don't know what to search
     
  9. filipetakanap

    filipetakanap

    Joined:
    Nov 16, 2021
    Posts:
    35
    It is working for the Y Axis! the Z keeps doing the same, not sure why:
    Code (CSharp):
    1. float distance_to_screen = Camera.main.WorldToScreenPoint(gameObject.transform.position).z;
    2.         Vector3 pos_move = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, distance_to_screen));
    3.         pos_move.x = Mathf.RoundToInt(pos_move.x / gridSizeX) * gridSizeX;
    4.         pos_move.z = Mathf.RoundToInt(pos_move.z / gridSizeZ) * gridSizeZ;
    5.         transform.position = new Vector3(pos_move.x, transform.position.y, pos_move.z);
    EDIT: It should move around only on X and Z axis. not up and down on Y. It is just to move around the terrain
    EDIT: Not sure what i have done, but it now moves, but only backwards xD
    Code (CSharp):
    1. float distance_to_screen = Mathf.RoundToInt((float)(Camera.main.WorldToScreenPoint(gameObject.transform.position).z));
    2.         Vector3 pos_move = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, distance_to_screen));
    3.         pos_move.x = Mathf.RoundToInt((pos_move.x / gridSizeX) * gridSizeX);
    4.         pos_move.z = Mathf.RoundToInt((pos_move.z / gridSizeZ) * gridSizeZ);
    5.         Debug.Log(pos_move.z);
    6.         gameObject.transform.position = new Vector3(pos_move.x, transform.position.y, pos_move.z);
     
    Last edited: May 31, 2022
  10. filipetakanap

    filipetakanap

    Joined:
    Nov 16, 2021
    Posts:
    35
    i found out that for it to work i need to push the camera upwards and rotate it downwards.. If you have better sugestions, please, say so.

    Thanks