Skip to content

Tutorials · Navigation and AI · Chapter 2

A map of the floor

15 minutes Uses: NavRegion, TileMap

By the end

  • nav, a Nav Region over the whole keep: Size 640, 352, Cell Size 8, Mask 1, Clearance 3
  • The map baked with the Bake button: 3520 cells, the walls off it, every doorway open
  • What a cell size costs, what makes a wall, and what the clearance does and does not do
On this page

A region over the keep

A walker that finds its own way needs a map of where it may walk. A Nav Region is that map: a rectangle of the world cut into square cells, each one floor or wall. A walker plans its way from cell to cell over it. The keep needs one region, over all of it.

  1. Select keep, press + Add node, and choose Nav Region. Rename it nav.
  2. Set its Position to 320, 176, the keep’s middle, and its Size to 640, 352, the whole keep.
  3. Set Cell Size to 8.
  4. Click Mask’s button. A list of the collision layers opens, every box ticked. Untick every box but the first, 0, so the button reads 0, and click outside the list. The value saved is 1.
  5. Set Clearance to 3. Leave Allow Diagonals ticked.
  6. Press Ctrl+S.
nav selected, the last row of the Hierarchy, and framed: a rectangle with a square handle at each corner round the whole keep, a fine grid of cells over every room and wall; the strip under the Viewport reads Navigation not baked beside a Bake button; the Inspector shows Position 320.0 and 176.0, and its NavRegionComponent with Size 640.0 and 352.0, Cell Size 8.00, Allow Diagonals ticked, Mask 0 and Clearance 3.00

The next sections say what each of those numbers is for. The rectangle is centred on its node, so a region at 320, 176, 640 by 352, runs from 0, 0 to 640, 352, the keep’s edges.

Size
The Nav Region’s Size: its width and height in world units, centred on its node. 640, 352 is the whole keep. Everything outside it is off the map; a region larger than the room only adds cells of rock.

The keep is seen from above, and the map is of a floor: it knows no up and down. A platformer whose enemies jump between ledges is not what it does.

Baking

The editor does not keep the map up to date by itself. Baking works it out: every cell is looked at, and every cell a wall stands on is taken off the map. The strip under the Viewport says where the bake is, in one of four words: Baking navigation, with how far it has got, while it runs; Navigation stale, in the warning colour, when something it read has changed since; Navigation not baked, before the first bake; and Navigation baked. In Play, before the first bake, the strip leaves the words out and shows Bake alone.

  1. With nav selected, press Bake on the strip, or press Ctrl+Shift+B. The strip reads Navigation baked, and the status bar at the bottom of the window says what the bake found: 3520 cells, 1304 of them blocked.
  2. Hover Navigation baked. It says the same, a line for each region: "nav": 3520 cells, 1304 blocked, 0 priced.
The keep baked, nav selected: every room and the corridor washed paler over a fine grid of cells, the walls between them left dark and unwashed, the doorways washed like the floor; the strip reads Navigation baked, and the status bar Baked navigation for nav: 3520 cells, 1304 blocked, 0 priced

The pale wash is the overlay: the map as the bake left it. A washed cell is floor a walker may cross, and a cell left as it is drawn is off the map. Every room is washed, and the walls are not.

The overlay
The Nav Region’s map as its last bake left it, drawn over the Viewport while the region is selected: open floor washed pale over the grid of its cells, and every cell off the map left as it is drawn.

Cells of 8

The keep’s walls are tiles of 16, so a map of cells of 16 would follow them exactly. Its tables and the middles of its doorways are half a tile off that grid, and a map follows the room only as finely as its cells. Cells of 8 are half a tile, so those fall on the map too.

The great hall baked in cells of 16: the hearth, the bell, the cat, the hall guard and the two tables under a coarse grid, each square of it a whole tile, the hall washed and its walls dark
For comparison, Cell Size 16: the great hall in cells twice as wide. The whole keep is 880 cells.
Cell Size
The Nav Region’s Cell Size: how wide and tall each of its cells is, in world units. A walker’s way runs from cell middle to cell middle, so a cell is the finest thing the map can tell apart. 8, half a tile, suits the keep; 16 would do for a room with nothing off the tile grid. Halving it makes four times the cells, here 3520 instead of 880, and every bake and every way found looks at all of them.
Allow Diagonals
Whether a walker on the Nav Region may step to the four cells corner to corner with its own, as well as the four beside it. Ticked, a way cuts across a room on a slant; unticked, it goes in steps along the grid. Leave it ticked here: the rat in chapter 13 has it unticked.

What blocks

The walls are tiles on the floor node’s tilemap, and a tile layer is a wall on the map when it says so. The starter’s walls layer already does.

  1. Select floor and click the Tilemap tab beside the Inspector. Its Layers list holds floor, walls and decoration. The button on the walls row reads SN: click it, and Solid and Blocks nav are ticked. Leave them so, click outside the list, and click the Inspector tab.
floor selected and the keep framed; the Tilemap tab beside the Inspector shows its Layers list, floor, walls and decoration, and the walls row’s SN button clicked open: a list of Solid ticked, One-way unticked, Blocks nav ticked and Occluder unticked, and under Collision layers, Every layer ticked
Blocks nav
A tile layer’s flag: its tiles are walls on every Nav Region whose Mask holds the layer’s collision layer. It is its own flag, not Solid: a layer of water could block the map and stop no one, and a solid layer can leave the map alone. A tile layer’s walls stand on every collision layer, so any Mask holds them.
Mask
Which collision layers wall off the Nav Region’s map: a solid Collider on a shown node, on a layer the mask holds, blocks the cells it covers, and one on any other layer does not. Its button lists the boxes ticked; a new region has every one. The keep’s is 1, which ticks one box, labelled 0: the layer a Collider stands on unless it says otherwise, and the layer anything you add to block the map goes on.

Walkers are not walls, whatever their layer. A Collider on a node that has a Character Body or a Nav Agent never blocks the map, or every walker would stand in a hole of its own making. A walker the others must go round says so with a Nav Obstacle, which chapter 3 adds.

The keep baked with the walls layer’s Blocks nav unticked: the whole keep washed, the walls washed grey instead of left dark; nav carries a warning sign in the Hierarchy, the status bar says 1 problem and Baked navigation for nav: 3520 cells, 0 blocked, 0 priced, and the Problems tab shows one warning on nav: This Nav Region’s bake blocked nothing, though 19 solid boxes stand in it, so walkers go straight through them — tick Blocks nav on the walls’ tile layer, or put the walls on a collision layer the region’s Mask holds
For comparison, the walls layer’s Blocks nav unticked: the bake blocks nothing, and the Problems tab says so.

A region whose bake blocked nothing while solid things stand in it is almost always a mistake, so the Problems tab names it after the bake, and the status bar counts it. The row goes once the walls block the map again and the region is baked.

The Problems row for a region that blocked nothing
A warning on the Nav Region after a bake that blocked no cell though solid boxes stand in it, so walkers would go straight through them. It counts the boxes and says what to change: Blocks nav on the walls’ tile layer, or a collision layer the region’s Mask holds. The region’s row in the Hierarchy carries a warning sign, and the status bar counts it.

Keeping off the walls

A walker’s way runs through cell middles, and the walker is wider than a point. The thief’s and the guards’ boxes are 8 wide. Clearance grows the walls, before the cells are marked, so a way keeps its distance from them.

Clearance
How far the Nav Region grows every wall and obstacle when it bakes, in world units. A cell whose middle the grown wall reaches is off the map. Half a walker’s width, 4 here, keeps its box off a wall; under half a cell, also 4 here, keeps a corridor one tile wide open. The keep’s is 3, under both: a walker on the map brushes a wall by a unit, and every one-tile doorway keeps its cells.
The kitchen door close, baked: the dark wall between the hall and the kitchen runs down the middle, broken by the door, and the cells in the doorway are washed like the floor on both sides; the thief stands in the kitchen at the bottom right
The kitchen door, seen through the game camera with the thief placed beside it: the doorway is one tile, two cells, and both are on the map.

The map

nav, a Nav Region under keep. Every other field is as a new Nav Region has it: Allow Diagonals ticked.

FieldValue
Position320, 176
Size640, 352
Cell Size8
Mask1: the box labelled 0
Clearance3

Check it works