Build a 2D Platformer using Unity and C#

Lesson 2 of 7

Tilemaps and Level Design

Hand-placing hundreds of individual sprite GameObjects for a level doesn't scale. Unity's Tilemap system lets you paint a level like a grid-based canvas instead.

Setting up a Tilemap

  1. Right-click in the Hierarchy → 2D Object → Tilemap → Rectangular, this creates a Grid GameObject with a Tilemap child.
  2. Open the Tile Palette window (Window → 2D → Tile Palette), create a new palette, and drag your tile sprites into it.
  3. Select the paint brush tool and click on the Tilemap in the Scene view to place tiles.

Making the tiles solid

On the Tilemap GameObject, add a Tilemap Collider2D and a Rigidbody2D set to Body Type Static. A TilemapCollider2D automatically generates a collider shape from whatever tiles are painted, so painting a new platform is enough, no manual collider work needed.

Reading tile data from code

using UnityEngine;
using UnityEngine.Tilemaps;

public class LevelInfo : MonoBehaviour
{
    public Tilemap tilemap;

    public bool IsSolidAt(Vector3 worldPosition)
    {
        Vector3Int cell = tilemap.WorldToCell(worldPosition);
        return tilemap.HasTile(cell);
    }
}

The pieces

  • Grid defines the cell size and layout that every child Tilemap shares, a level can have several Tilemaps (ground, background decoration, hazards) stacked on the same Grid.
  • A Tilemap's Rigidbody2D should be Static, the level geometry doesn't move.
  • WorldToCell converts a world-space position (like a GameObject's transform.position) into the Tilemap's grid coordinates, useful for gameplay logic that needs to know "what tile is under this point."

TIP

Keep hazards (spikes, lava) on a separate Tilemap from solid ground, with its own collider set to Is Trigger. That way a single OnTriggerEnter2D check on the player can detect "touched a hazard" without it also being solid ground.

📝 Tilemaps Quiz

Passing score: 70%
  1. 1.Which component automatically generates a collider shape that matches the tiles painted on a Tilemap?

  2. 2.A Tilemap's Rigidbody2D should be set to Body Type ____, since level geometry doesn't move.

  3. 3.A single Grid GameObject can have multiple Tilemap children, e.g. one for ground and one for background decoration.