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

How to get the world coordinates of Tile points?

Discussion in '2D' started by manking99, Sep 24, 2021.

  1. manking99

    manking99

    Joined:
    May 29, 2021
    Posts:
    11
    Good day!
    How to get the world coordinates of Tile corner points (hex point top)?

    upload_2021-9-24_16-18-3.png


    My code:

    Code (CSharp):
    1. Vector3Int Vector3IntPoint = new Vector3Int(4,8); // cell
    2. Vector3 worldCenterPos = MyTileMap.CellToWorld(Vector3IntPoint);
    3.  
    worldCenterPos - this is center point?
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,969
    Use something like OnDrawGizmos() to find out what that position really is. From there you can use the dimensions of the hexes to find what else you need.
     
    manking99 likes this.
  3. Lo-renzo

    Lo-renzo

    Joined:
    Apr 8, 2018
    Posts:
    1,323
    You want to use new Vector3(4.5, 8.5) then use tilemap.CellToLocalInterpolated(Vector3 someInterpCell). This method takes a Vector3 instead of Vector3Int so you may specify that you want the center of the cell.

    However, its drawback is that it returns the Local not World position. For a tilemap with a transform at (0,0,0) then the local position is the same as the world position. But when a tilemap is not zero-centered, then they diverge so keep that in mind.

    That said, I've never worked with hex (I know my advice works for rectangular and isometric tilemaps), and there's a chance that (.5, .5) isn't the cell center for hex, so Kurt's suggestion of double checking with gizmos will tell you more information.
     
    manking99 likes this.
  4. manking99

    manking99

    Joined:
    May 29, 2021
    Posts:
    11
    How do I get the top hex point?

    Vector3(4.5, 8.5) - center.
    Vector3(4.5, 8.999) - top point of 4x8 cell?

    Maybe with the center and top points, we can rotate something to get the next hex points?
     
  5. Lo-renzo

    Lo-renzo

    Joined:
    Apr 8, 2018
    Posts:
    1,323
    Use this (untested from memory):
    Code (CSharp):
    1. public class MousePrinter : MonoBehaviour
    2. {
    3.     public Tilemap tilemap; // assign tilemap thru Inspector, make sure its transform.position is (0,0,0)
    4.     public Vector3 mousePosWorld;
    5.     public Vector3Int mouseCell;
    6.     public Vector3 mouseCellInterpolated;
    7.  
    8.     void Update()
    9.     {
    10.         mousePosWorld = Camera.main.ScreenToWorldPoint(Input.mousePosition);
    11.         mouseCell = tilemap.WorldToCell(mousePosWorld);
    12.         mouseCellInterpolated = tilemap.LocalToCellInterpolated(mousePosWorld); // this gives interpolated (Vector3) cellspace
    13.     }
    14. }
    It's important to note I'm assuming world = local so (0,0,0) necessary for tilemap and all its parents' positions.

    This should give you the cellspace positions of each of the corners of a the hex cell if you mouse over it. We translate from screenspace (the mouse) to worldspace (transforms) to cellspace (the hex grid).