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. Dismiss Notice

Question How do I get a tile from a tilemap set as trigger ?

Discussion in '2D' started by f0Cs, Jul 5, 2023.

  1. f0Cs

    f0Cs

    Joined:
    Jan 28, 2023
    Posts:
    5
    I'm making a 2D game where I use Tilemaps. One of them is set as trigger, and when the player collides with a tile from this Tilemap, I want to get said tile. I know I can do that with GetTile if I know where the tile is. However:
    - I can't use the Tilemap Collider position because it returns the entire tilemap, not the tile the player collided with
    - I can't use OnCollisionEnter2D to get the collision location, since this is a trigger
    - I can't use any classes that requires the location of the tile as a parameter, that's literally what I'm looking for.

    Even after looking at a few posts and reading documentation, I still haven't found any satisfying answers. Do you have any idea about how to resolve this ?
     
    Last edited: Jul 5, 2023
  2. vonchor

    vonchor

    Joined:
    Jun 30, 2009
    Posts:
    238
    You could use the bounds of the collision and the bounds of the tile's collider - see if they intersect.

    collisiondata.collider.bounds is the collider bounds (let's call this CB) and you can get the tile sprite's bounds (SB) for all tiles in the general area of the collision.

    Then
    CB.Intersects(SB)

    iterated over all the tiles in the collider region will tell you which tile is at the collision point.

    It's more complex than that, but it's the basic idea. I can point you to a demo that may help, if you want.
     
  3. f0Cs

    f0Cs

    Joined:
    Jan 28, 2023
    Posts:
    5
    Yes, I'd appreciate it if you could send me an example ! It would make it much easier for me to understand what you're saying.
     
  4. vonchor

    vonchor

    Joined:
    Jun 30, 2009
    Posts:
    238
    I have a free asset (Asset Store) called TilePlus Toolkit. Check out the 'Collision Demo'

    It's not 100% what you're looking for but should give you the general idea.
     
  5. f0Cs

    f0Cs

    Joined:
    Jan 28, 2023
    Posts:
    5
    I think I get it now... But how can I get the bounds of the tile sprite around the area of collision ?
     
  6. vonchor

    vonchor

    Joined:
    Jun 30, 2009
    Posts:
    238
    TileUtil.GetTrueBoundsForTileSprite

    in Runtime/StaticLib/TileUtil

    but here it is:

    Code (CSharp):
    1.  /// <summary>
    2.         /// Get the true bounds for a Tile's sprite. Takes into account transform scaling.
    3.         /// </summary>
    4.         /// <param name="map">Source tilemap</param>
    5.         /// <param name="position">position</param>
    6.         /// <returns>Bounds for sprite. If map==null returns an empty Bounds </returns>
    7.         public static Bounds GetTrueBoundsForTileSprite(Tilemap? map, Vector3Int position)
    8.         {
    9.             if (map == null)
    10.                 return new Bounds();
    11.  
    12.             //Need the world position of the tile to create a bounds.
    13.             var worldPos = map.GetCellCenterWorld(position);
    14.          
    15.             //Get the sprite from the tilemap. If one were to obtain this from the Tile then it might be a tilesheet.
    16.             var sprt = map.GetSprite(position);
    17.          
    18.             //Get the scale of the Tile sprite at that position.
    19.             var scale = GetTransformScale(map, position);
    20.             if(scale == Vector3.one)
    21.                 return new Bounds(worldPos, Vector3.one);
    22.  
    23.             //Get the TRUE size of the sprite
    24.             var spriteSize = Vector3.Scale(sprt.bounds.size, scale);
    25.  
    26.             return new Bounds(worldPos, spriteSize);
    27.         }
    28.    
    which uses this (from the same TileUtil class):

    Code (CSharp):
    1. /// <summary>
    2.         /// Get the scale of a tile's sprite
    3.         /// </summary>
    4.         /// <param name="map">the tilemap</param>
    5.         /// <param name="position">position of tile</param>
    6.         /// <returns>the scale of the sprite</returns>
    7.         // ReSharper disable once MemberCanBePrivate.Global
    8.         public static Vector3 GetTransformScale(Tilemap map, Vector3Int position)
    9.         {
    10.             return map.GetTransformMatrix(position).lossyScale;
    11.         }
     
  7. f0Cs

    f0Cs

    Joined:
    Jan 28, 2023
    Posts:
    5
    Mmh. For some reason, I can't seem to find it... Am I doing something wrong ?
     

    Attached Files:

  8. f0Cs

    f0Cs

    Joined:
    Jan 28, 2023
    Posts:
    5
    Nevermind, in the end I simply used Raycast around the player's collider and used hit.point to get the location of the tile. Still, thanks for your help!