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

Isometric tilemap : get specific tile on click

Discussion in 'Scripting' started by trololo, Dec 30, 2019.

  1. trololo

    trololo

    Joined:
    Dec 13, 2012
    Posts:
    17
    Hi, I know that subject have been discussed quite a lot, but I have not found any proper answer except some shy "it's not possible" (which might be true after all).

    My problem occurs when sprites spread over multiple tiles. Because otherwise I can use that short piece of code and it gives me the right tile :
    Code (CSharp):
    1. Vector2 mousePos = cam.ScreenToWorldPoint(Input.mousePosition);
    2. Vector3Int gridPos = tilemap.WorldToCell(mousePos);
    3. if (tilemap.HasTile(gridPos))
    4. {
    5.     Debug.Log(tilemap.GetTile(gridPos).name);
    6. }
    So I'm currently using a Raycast in order for my click to be detected depending on the sprite collider (see attached file). So far I'm using that script that is detecting the collision properly when I'm clicking on one of the assets in that tilemap :
    Code (CSharp):
    1. Ray ray = cam.ScreenPointToRay(Input.mousePosition);
    2. RaycastHit2D hit = Physics2D.GetRayIntersection(ray, Mathf.Infinity);
    3.  
    4. if (hit.collider != null)
    5. {
    6.     Debug.Log(hit.collider.gameObject);
    7. }
    Unfortunatly, this is a returning the whole tilemap and I am not able, one way or another, to get tile's information (at least the name of the generic sprite).
     

    Attached Files:

    Last edited: Dec 31, 2019
  2. adi7b9

    adi7b9

    Joined:
    Feb 22, 2015
    Posts:
    181
    Maybe if you get the whole list of colliders on raycasthit then you have your info.
     
  3. tonemcbride

    tonemcbride

    Joined:
    Sep 7, 2010
    Posts:
    1,078
  4. trololo

    trololo

    Joined:
    Dec 13, 2012
    Posts:
    17
    @adi7b9 the thing is there is only one Tilemap collider attached to the tilemap; so in my understanding there is no way to get the sprites colliders as independent entities.

    @tonemcbride I'm using that function already in my first example; but, in case of a large sprite spreading over several cells again, when I convert a successfull raycast to a cell, that cell might not be part of the tilemap (because whatever the size of the sprite it only "belongs" to one reference tile).

    I think my best chance here is to define a gameObject with a polygon collider for each clickable asset in my scene (which is quite time consuming).

    Thanks for your answers.
     
  5. tonemcbride

    tonemcbride

    Joined:
    Sep 7, 2010
    Posts:
    1,078
  6. trololo

    trololo

    Joined:
    Dec 13, 2012
    Posts:
    17
    I would bump into the same problem. Take a look at the following screenshot :
    cat.png
    The orange bordered tile is the reference tile for the asset. If I click on the head of the cat, my raycast will hit the collider and return true. But if I then convert the hit point to a tilemap position (with the WorldToCell function), the corresponding cell is not the reference one, so it does not belong to the tilemap and therefore tilemap.HasTile returns false (GetTile or GetSprite would return a null reference).
     
  7. tonemcbride

    tonemcbride

    Joined:
    Sep 7, 2010
    Posts:
    1,078
    Ah, ok - I'd probably need to try it myself to see the problem. If you want to create and post a basic test project showing the problem I can have a look for you sometime.
     
  8. trololo

    trololo

    Joined:
    Dec 13, 2012
    Posts:
    17
    Last edited: Jan 3, 2020
    adi7b9 likes this.
  9. indexhever

    indexhever

    Joined:
    Mar 30, 2012
    Posts:
    6
    Did you manage to get the tile by clicking? I want to do something similar, like a tile palette in runtime, so the player can add and edit objects in the scene...