Hello Guest

Author Topic: How to get the current clicked tile on a tile map?  (Read 5169 times)

night_dog

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 5
    • View Profile
How to get the current clicked tile on a tile map?
« on: August 22, 2013, 09:51:26 pm »
Hi. Is there an API which I can use to get the current clicked tile on the tile map? I have a character which is placed on top of a tile map and I want to be able click somewhere on the map and move the player towards the clicked point. Also I want to know exactly which tile is clicked.


night_dog

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 5
    • View Profile
Re: How to get the current clicked tile on a tile map?
« Reply #1 on: August 22, 2013, 09:59:24 pm »
Is there a solution without enabling caps on the sprite collection? Could you give me some guidelines what script will be needed?
I've tried with front and back polygon collider set, but it seems it is not working. This is the script which I have but it is not working:
Code: [Select]
if (Input.GetButtonDown("Fire1"))
        {
            Ray ray = gameCam.ScreenPointToRay(Input.mousePosition);
            RaycastHit hit = new RaycastHit();

            if (Physics.Raycast(ray, out hit))
            {
                Debug.Log("test");
            }
        }
« Last Edit: August 22, 2013, 10:07:09 pm by night_dog »

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: How to get the current clicked tile on a tile map?
« Reply #2 on: August 22, 2013, 10:10:16 pm »
This post shows you how you can get the x, y position of a click / touch on a tile map without colliders.
http://unikronsoftware.com/2dtoolkit/forum/index.php/topic,2304.msg11501.html#msg11501

This is WAY more efficient than using colliders.

night_dog

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 5
    • View Profile
Re: How to get the current clicked tile on a tile map?
« Reply #3 on: August 24, 2013, 07:50:48 am »
Thanks. This will work. Just one more question. :) Is it possible to get all the tiles for a given layer? I've checked the class tk2dTileMap but it has no methods which will return such results. There is a property TilePrefabsList which is not documented yet, in my case it contains no items, and probably will give only the prefabs and not the actual tiles.

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: How to get the current clicked tile on a tile map?
« Reply #4 on: August 24, 2013, 12:00:27 pm »
for (int x = 0; x < tilemap.width; ++x) {
for (int y = 0; y < tilemap.height; ++y) {
  tilemap.GetTile(x, y, layer);
}
}

Wouldn't that do?