In order to let the player interact with the environment created in the Tilemap, we need to add colliders . For example, if the player needs to walk on the ground and not fall through, then the entire ground assets need to have a collider. Tilemap colliders simplify this process . Let’s dive in and see how to use this feature.
1. Create scene with Tilemaps
This is the new Scene that I created , having 2 Tilemaps, Foreground and Background. The Inca stone blocks are all on the Background Tilemap , and the remaining elements are on the Foreground Tilemap
2. Setting up Tilemap Collider
Select the background Tilemap, and in the Inspector panel add a new Component called Tilemap Collider. You will be able to see the colliders on all Tile Assets in the Tilemap , highlighted in green.
3. Remove unneeded colliders using Composite Collider 2D
If you notice above, there are colliders separating each individual tile in the background Tilemap. This is rather unhelpful, as we only need the colliders on the surface. To achieve this , we will need to add a Composite Collider 2D component
Of course, in case of games like Pong, the individual colliders would be most helpful, but for our scenario, we would only like to see the collider on the surface.
Lets go ahead and add a new Composite Collider 2D to the Background Tilemap . It creates 2 components – one Rigidbody 2D and one Composite Collider 2D
4. Configuring the Tilemap to use the Composite Collider
In the Tilemap Collider 2D, we need to select the check box “Used By Composite” . This will instantly transform the collider on the background tilemap into the uniform collider on the surface.
Also, we can set the Rigidbody 2D Body type to “Static” , since we are configuring the floor .
And that’s it, you should be good to go . Makes life so much easier than manually creating the colliders one by one for surfaces.
Happy level designing !