Search Unity

  1. Calling all beginners! Join the FPS Beginners Mods Challenge until December 13.
    Dismiss Notice
  2. It's Cyber Week at the Asset Store!
    Dismiss Notice

Best way to detect individual "Tilemap Collider 2D" colliders

Discussion in '2D' started by Gregwar, Feb 17, 2019.

  1. Gregwar

    Gregwar

    Joined:
    Jan 5, 2019
    Posts:
    9
    Hey all,
    I'm trying to detect if there are colliders around my ai character as part of its obstacle avoidance code. The problem is, it doesn't look like I'm able to detect the individual colliders of a single "tilemap collider 2d".

    If I perform a CircleCastAll, which encompasses 2 unique squares of the tilemap collider, only one collision point will be returned. Similarly if I use "OverlapCircleAll", the singular tilemap object will get returned.

    Is there any way to return multiple collision points from the same "Tilemap Collider 2D"? I want my ai character to know that there is a collider both above and below it even though the colliders are part of the same Tilemap.
     

    Attached Files:

  2. RidgeWare

    RidgeWare

    Joined:
    Apr 12, 2018
    Posts:
    65
    Unfortunately raycasts only return a result per object - and since the tilemap collider is attached to one tilemap object, it will never log multiple hits. It just logs the first hit it receives.

    If all you want is to know is whether tiles are nearby, one way around it is to keep a constant list of nearby grid positions in relation to the player position (the list object would be a class/struct which contains an x & y integer). And then use GetTile to look at the tile in that tilemap.

    For example, to represent a circle you might have 20-odd entries in the list like: {1, 0}, {2, 0}, {-1, -1},{-2,-1}, etc... and you would simply have a method which adds these x/y values to your player object x/y value each time it moves and stores the results. So if your player is at {7,5} on the grid, checking the first entry in the list would look at {8,5} - one tile to the right, then {9, 5}, 2 tiles to the right. Until you've checked all tiles within the circle.

    Each time the GetTile result isn't null, you know there's a tile in that position.

    Of course that's just one solution. There may be simpler ways.

    (Another way would be to fire multiple individual raycasts in several directions, but again you'd have the problem of not detecting tiles which are directly behind other tiles).
     
    Last edited: Feb 17, 2019
    Gregwar likes this.
  3. Gregwar

    Gregwar

    Joined:
    Jan 5, 2019
    Posts:
    9
    That's a great idea, I'll give it a try today! Thanks!
     
  4. Gregwar

    Gregwar

    Joined:
    Jan 5, 2019
    Posts:
    9
    Yup. That worked great
     
  5. RidgeWare

    RidgeWare

    Joined:
    Apr 12, 2018
    Posts:
    65
    Cool. Glad to help.

    Checking GetTile for null is a great little way to check surroundings in tilemap games, and I think it's pretty efficient too.