Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I get the x,y coordinate location of a tile in the tilemap?

Tags:

unity3d

I have just started getting used to using Unity's new tilemap tool (UnityEngine.Tilemaps).

One issue I am having is that I don't know how to get the x,y coordinates of a placed tile via script. I am trying to move a scriptableObject on the tilemap in script to a new location that the player clicks, but I don't know how to get the coordinates of the clicked tile location. There doesn't seem to be any position property for the Tile class (the Tile knows nothing about its location), so the Tilemap must have the answer. I was not able to find anything in the Unity documentation for how to get the Vector3 coordinates for a selected tile in the Tilemap.

like image 399
jeffrey Avatar asked Jan 16 '18 08:01

jeffrey


People also ask

How does Tilemap collider work?

The Tilemap Collider 2D component generates a Collider shape based on the Sprite assigned to the Tile. The Collider shape can either be the custom physics shape set for the Sprite or an approximation generated from the outline of the Sprite.

How do tile maps work?

A Tilemap consists of a grid overlay and a number of other components working together. The entire system allows you to paint levels using tiles and brush tools and define rules for how tiles behave. With Tilemaps, you can create platforms with dynamic edges, animated tiles, randomized tile placements, and more.

What is Tilemap?

Tilemaps are a very popular technique in 2D game development, consisting of building the game world or level map out of small, regular-shaped images called tiles.


2 Answers

If you have access to the Tile instance you can use its transform (or the raycast hit from the click) to get its world position and than get the tile coordinates via the WorldToCell method of your Grid component (look at the documentation).

EDIT:

Unity seems to not instantiate the tiles but instead uses only one tile object to manage all tiles of that type, i wasn't aware of that.

To get the correct position you have to calculate it yourself. Here is an example of how to get the tile position at the mouse cursor if the grid is positioned at the xy-plane with z = 0

// get the grid by GetComponent or saving it as public field
Grid grid;
// save the camera as public field if you using not the main camera
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
// get the collision point of the ray with the z = 0 plane
Vector3 worldPoint = ray.GetPoint(-ray.origin.z / ray.direction.z);
Vector3Int position = grid.WorldToCell(worldPoint);
like image 69
Shirotha Avatar answered Sep 28 '22 05:09

Shirotha


I couldn't find a way to get the Grid's position from mouse click, so instead I used a Raycast to Vector3, and then converted that to coordinates via the WorldToCell method of the Grid component as per Shirotha's suggestion. This allows me to move the selected GameObject to a new position.

public class ClickableTile : MonoBehaviour
{
    public NormalTile normalTile;
    public Player selectedUnit;

    private void OnMouseUp()
    {
        // left click - get info from selected tile
        if (Input.GetMouseButtonUp(0))
        {
            // get mouse click's position in 2d plane
            Vector3 pz = Camera.main.ScreenToWorldPoint(Input.mousePosition);
            pz.z = 0;

            // convert mouse click's position to Grid position
            GridLayout gridLayout = transform.parent.GetComponentInParent<GridLayout>();
            Vector3Int cellPosition = gridLayout.WorldToCell(pz);

            // set selectedUnit to clicked location on grid
            selectedUnit.setLocation(cellPosition);
            Debug.Log(cellPosition);
        }
    }
}

On an aside, I know how to get the Grid location, but now how to query it. I need to get the GridSelection static object from Grid, and then get its position.

like image 32
jeffrey Avatar answered Sep 28 '22 05:09

jeffrey