Search Unity

Get specific Tile from Tilemap after Click/Touch

Discussion in '2D' started by Zumpel, May 30, 2018.

  1. Zumpel

    Zumpel

    Joined:
    May 30, 2018
    Posts:
    7
    Hello guys.

    I'm new to Unity and currently doing a 2D Puzzle Mobile Game. For this Project I need to "draw" on the Screen. You can imagine it like drawing a path from A to B. So the Idea is to make an Input (doesn't matter right now if its Touch or Click) on my Tilemap, to get a specific Tile. This Tile will be then inserted in the Pathlist of my Player.

    The problem: I can't find a good (well scaling) approach. I really don't want to calculate it with Tilesizes and and such stuff, since it's a mobile Game with different resolutions. So my approach was to use Raycasts at my Tilemap, which only returns the entire Map Object and not a specific Tile.

    I would be glad if somebody has an Idea to fix my problem.

    Thanks and Greetings!
     
  2. Zumpel

    Zumpel

    Joined:
    May 30, 2018
    Posts:
    7
    I've read into the Documentation and got a solution because of a friend.
    You just have to create a Ray, calculate the Grid Position and then extract the Tile with the help of "GetTile".

    Code (CSharp):
    1.             Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
    2.             Vector3 worldPoint = ray.GetPoint(-ray.origin.z / ray.direction.z);
    3.             Vector3Int position = m_level.WorldToCell(worldPoint);
    4.  
    5.             TileBase tile = m_level.GetTile(position);
     
    karlazamora and eclipse130300 like this.
  3. TremblingGigan

    TremblingGigan

    Joined:
    Jan 21, 2019
    Posts:
    1
    I know this is a year old, but do you remember what m_level is? I assumed it was GridLayout however I do not see a GetTile function for GridLayout, is it instead Tilemap?
     
  4. JJunjo

    JJunjo

    Joined:
    Dec 2, 2015
    Posts:
    3
    @TremblingGigan, m_level should be the Tilemap object that contains the tile in the scene.