2D Toolkit Forum

2D Toolkit => Support => Topic started by: night_dog on August 22, 2013, 09:51:26 pm

Title: How to get the current clicked tile on a tile map?
Post by: night_dog 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.

Title: Re: How to get the current clicked tile on a tile map?
Post by: night_dog 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");
            }
        }
Title: Re: How to get the current clicked tile on a tile map?
Post by: unikronsoftware 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.
Title: Re: How to get the current clicked tile on a tile map?
Post by: night_dog 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.
Title: Re: How to get the current clicked tile on a tile map?
Post by: unikronsoftware 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?