Set up tilemap collision
Tutorial
·
Beginner
·
+0XP
·
20 mins
·
(1307)
Unity Technologies

In this tutorial you’ll implement tilemap collision to restrict character movement.
By the end of the tutorial, you’ll be able to do the following:
- Configure tilemap collision.
- Optimize the tilemap collider.
- Place additional objects in the scene to restrict player movement using the prefab system.
Languages available:
1. Overview
You’ve now configured the movement in your game so that the player can be blocked by an object by using collision detection. There are parts of your tilemap that you might also want to restrict the player from moving over — for example, water in the Ruby’s Adventure asset pack or icing in the Sugar World assets.
Restricting character movement is a key part of level design. You can block movement to guide your player towards a particular destination or to create challenges for them to navigate, such as a bottleneck where a dangerous enemy is located.
By the end of this tutorial, you’ll be able to do the following:
- Configure tilemap collision.
- Optimize the tilemap collider.
- Place additional objects in the scene to restrict player movement using the prefab system.
Working on your own project?
This tutorial is part of Beginner 2D: Adventure Game and refers to the game project for that course, but this information will be helpful for any beginner creator who wants to set up basic tilemap collision in their 2D game.
Note: For the purposes of this tutorial, we chose to use the Ruby’s Adventure asset set, and the file paths used in instructions will reflect this. If you chose another asset set, the file names will be the same, but in your corresponding theme folder.
2. Create a tilemap collider
Let’s focus on the tilemap first; once you’ve configured tilemap collision, you can create and place more decorative objects to enhance your game environment.
You can restrict the player’s movement over a particular area of the gameplay environment by creating an empty GameObject, adding a collider, and positioning it over the tilemap. However, this approach is very inefficient and could result in errors that are difficult to fix.
Instead, you can create a specific collider for your tilemap. When you have determined which tiles a moving GameObject should collide with, the tilemap collider will create the colliders for the tiles that need it.
To set up a tilemap collider, follow these instructions:
1. In the Hierarchy window, select the Tilemap GameObject.

2. In the Inspector window, select Add Component.
3. Search for “Tilemap Collider 2D” and select this component. By default, all tiles that you have not otherwise configured will have a collider.
4. In the Project window, within your asset pack folder, navigate to Art > [Your Chosen Project] > Tiles. Select all the tiles and rule tiles that you want the player to be able to move over freely — in this case, this is every tile except the ones containing water (or the water-equivalent in your asset pack).
Note: You can use the Ctrl key (macOS: Cmd) and click to select the correct tiles individually.
5. In the Inspector window, set Collider Type to None, and then save your changes.
6. If your PlayerCharacter GameObject is positioned over or next to water water tiles, move it slightly away on tiles that don’t have collision set up. This adjustment will avoid any issues when you test your changes.
7. Enter Play mode to test your changes – the player character should no longer be able to move through the tiles that have colliders.
3. Optimize the tilemap collider
Although the TileMap Collider 2D component is managing collision for the tilemap, right now each tile has its own collider if it needs one. This approach works, but it does cause the following two problems:
- The physics system needs to perform a lot of calculations. If you create a large game world for the player to explore, this could impact the performance of your game for players, especially if they have low-specification hardware.
- Errors can occur when the player moves across the border between tiles. Each tile will have its own collider if it needs one, which means there will be tiny gaps between the colliders. These gaps can cause rare issues of collision occurring when it shouldn’t.
You can address these issues by adding a Composite Collider 2D component. This component creates one large collider from all the colliders on the GameObject that it is attached to — or the child GameObjects of that GameObject, as in this situation.
To configure this component and optimize your tilemap collider, follow these instructions:
1. In the Hierarchy window, select the Tilemap GameObject.

2. In the Inspector window, select Add Component.
3. Search for “Composite Collider 2D” and select this component. Unity will add a Rigidbody 2D component too, because the Composite Collider 2D component needs a Rigidbody component on the same GameObject to work properly.
4. In the Tilemap Collider 2D component, enable the Used By Composite property.

5. In the Rigidbody 2D component, set the Body Type to Static.

This setting helps the physics system optimize its calculations, because it now knows that the Tilemap GameObject cannot move.
6. Enter Play mode to test your changes.
The player character’s movement will be just like it was at the end of the previous step. If you update your tilemap, Unity will automatically update the Tilemap GameObject’s composite collider to reflect the colliders of the new tiles.
Important: Remember to exit Play mode when you’ve finished testing.
4. Add colliders to other decorative objects to your environment
In the previous tutorial you created decorative object prefabs and placed them around the game environment. If you want the player character to collide with any of those decoration types, all you need to do is add 2D collider components to the prefabs and adjust the size.
If you need a guide to help you to configure your decorative objects, follow these high level instructions:
1. Open the prefab in prefab editing mode. If you don’t do this, you’ll be overriding the configuration for one particular instance of the prefab.
2. Add a Box Collider 2D component to the prefab.
3. Adjust the collider size to cover the following:
- The full width of the decoration sprite.
- Half the height of the sprite.
4. Save your changes.
5. Reposition instances of the prefab if you need to.
Remember, if you place the decorative objects outside the camera bounds, they will not be visible in Play mode at the moment.
6. Test your game after placing your objects to check that everything is as you expect.
5. More things to try
If you want to further develop your skills, explore new concepts, or improve your project, check out some of the optional activities below. Each one is tagged as either Easy, Medium, or Difficult, so you can choose the level of challenge.
These activities are entirely optional, so if you’re not interested, no problem — just skip this step. We do recommend attempting at least one of them in order to get the most out of this learning experience. Good luck!
Easy: Experiment with water placement in your game
Think about the player’s experience of moving their character around the environment. How can you use water, or your asset pack’s equivalent, to impact that experience? Try out different design adjustments and consider how you can integrate no-movement areas into the gameplay experience.
6. Next steps
This tutorial brings to a close most of your work on the environment for your game, though you can always update it later. Next you’ll move on to create a health system for the player character, which you’ll use to both deal and heal damage.