Adding Colliders to Tilemaps in Unity
Adding a collider to your Unity tile map is extremely easy and can be done in a couple of steps. This short article will demonstrate the process.
First add a tilemapcollider2d component to your tilemap with the add component button in the inspector. You can see what this does below:
What has happened is that every tile on the tilemap receives its own collider. It would be better if we had one collider that was a composite of all the tiles in the tilemap. This can be done by adding a compositecollider2d component to your tilemap like so:
Once you add the composite collider component, Unity will also add a rigidbody2d component, which depends on the composite collider. Change the rigidbody2d’s body type to static and then on the tilemap collider 2d component check the box next to used by composite. This can be seen above. What happens is that a collider will be created, which is essentially a composite of all the little colliders on each tile.
To demonstrate that our collider is working, I have added a square to the scene and given it a rigidbody2d and a collider2d so that it will fall to and collide with the platform below:
That’s all it takes to get a basic collider set up on your tilemap.