I’m developing a 2d sprite based game where when the player moves they can either push or destroy the tile they are colliding with. Think DigDug where you destroy the dirt or push a Rock. My thought was to use TileMaps to quickly lay out levels then listen for OnTriggerEnter2D on each tile to determine how to handle the collision. But I don’t think that’s how TileMaps work? It seems the TileMap is one big collider so I only get events fired for colliding with the whole thing and not individual grid cells/tiles. Is there a way to get notified of entering individual tiles in the TileMap? Is there some other method besides TileMaps I should be using besides manually laying out all my tiles with their own colliders?
TilemapCollider2D is a single collider component containing all the shapes in the tilemap. Just fyi, you can use a CompositeCollider2D to actually combine adjacent shapes into contiguous surfaces.
You can also have individual Tile colliders by creating custom Tiles. Tiles are extremely customizable and can be whatever you need them to be.
Check out this demo of the capabilities of TIleMap, Tiles, and Brushes.
You can use multiple tile maps under one grid and each one of these can have its own tag, collider etc. So what I have done is have walls on one tile map, floor on etc. This way when my player hits the wall then the trigger is fired and I then check to see what tag the collider has. The floor is not set with a collider at all as it is purely aesthetic.
It just occurred to me that if you don’t need any special functionality, could use the contact point of the collisions with the Tilemap, along with the function TileMap.GetTile to get the specific tile you collided with to change/remove. You may need to offset the contact point so that it sits within the collider, otherwise you’ll get the tile your character is standing in.
@LiterallyJeff that’s the approach I was going for, the problem is once your player is inside the TileMap you don’t get any events triggered. Technically I could use Layers and I should never enter any tile within my “moveable” layer, but I think my problem is I’m trying to make TileMaps do something they weren’t intended for. Basically each of my tiles should be an individual object and slide around independently of anything else. I did some digging around and found GridEditor2D Unity Asset Store - The Best Assets for Game Making which lets you place sprites in a uniform grid, each with their own object/collider. This seems to do what I want.
You’re not using TileMap in a way it’s not intended for. You can absolutely create tiles that get special physics, or full layers that are collidable or not. You can use Tilemap for just a grid layout, or as a full game logic interface.
You would need one background layer that you walk around in front of, another layer for the collidable walls, another for destructable tiles. You can make a tile that spawns a physics object that slides around. It’s very flexible.
You could even just use the Grid component on its own for placement and grid sizing/spacing:
However if that asset fulfills your needs, then by all means use it. Personally I try to avoid pulling in outside code and plugins whenever possible to reduce dependencies on 3rd parties, and in my opinion Tilemap makes those assets obsolete.
Just wanted to follow up on this and show you this script from the 2D Extras package that Unity offers in their github. I wasn’t aware they had released these yet.
This brush allows you to paint prefabs into the tilemap, there’s also a GameObject brush.
https://github.com/Unity-Technologies/2d-extras
Brickbreaker and Destruction examples:
https://github.com/Unity-Technologies/2d-techdemos