Skip to content

Tutorials · Navigation and AI · Chapter 5

Walking it

15 minutes Uses: NavAgent, PathFollow

By the end

  • A Nav Agent on cat that walks it from the hearth to its bowl as the game starts, through the kitchen door, facing its walk
  • Its Path Follow removed: one mover on the node, and the Problems row that two raise
  • Its route seen in Play, and the cat sent down the hole to the cellar, where there is no way back up
On this page

A walker

Chapters 2 to 4 made a map, and nothing walked it. A Nav Agent walks its node to a point over that map, by the cheapest way the map offers: round walls and tables, along roads, across links. It needs no script. The cat gets one, to walk from the hearth to its bowl in the kitchen.

  1. Select cat in the Hierarchy. Click Add component and choose NavAgentComponent.
  2. Set Target: click its x box once, so that its number is selected, type 424 and press Enter; then click its y box once, type 164 and press Enter. That is the bowl, in the kitchen.
  3. Tick Moving. Set Speed to 30 and Arrive Distance to 6 the same way, and tick Face Walk. Use Links stays ticked.
cat selected, its Inspector narrowed with the search box, navagent, to its Nav Agent: Target 424.0 and 164.0, Moving ticked, Speed 30.00, Arrive Distance 6.00, Face Walk ticked, Use Links ticked; in the Hierarchy the cat’s row carries a warning triangle, the status bar says 1 problem, and the Viewport shows the keep with the hall round and the hearth loop drawn
The cat’s Nav Agent. The triangle on its row, and the status bar’s 1 problem, are the next section’s.
Target
The Nav Agent’s Target: where it walks to, in world units, in the world, not from its node or its parent. The cat is still hearth_loop’s child, and its Target is the bowl all the same. A Target inside a wall is walked to the nearest floor. The way ends on the Target itself when its cell is floor. A new agent’s is 0, 0.
Moving
The Nav Agent’s Moving: whether it walks to its Target now. Ticked, it plans a way and walks it; it unticks itself when it arrives, and when the map has no way there. Untick it to stand the walker where it is. A new agent’s is unticked, and stands until told; the cat’s is ticked, so it sets off as the game starts.
Speed
The Nav Agent’s Speed: how fast it walks, in world units a second, and how fast it is carried along a link that is not a teleport. 30 is a cat in no hurry: the thief runs at 70. A new agent’s is 80; it cannot go under 0.
Arrive Distance
The Nav Agent’s Arrive Distance: how near its Target counts as there, in world units. Within 6 of the bowl the cat stops and Moving goes off. Under a cell, 8 here, suits the keep: much more and a walker stops short of where you sent it. A new agent’s is 4; it cannot go under 0.5.
Face Walk
The Nav Agent’s Face Walk: ticked, it mirrors the node’s Sprite to face left while the node walks left, and back while it walks right. The cat is drawn side-on, facing right, so it faces its walk. A new agent’s is unticked, which suits a picture seen from above.
Use Links
The Nav Agent’s Use Links: whether its way may cross Nav Links. Ticked, as a new agent has it and the cat keeps it, the hole and the ladder are part of its map. Unticked, it walks floor only, and the gallery and the cellar are out of its reach. Chapter 6 unticks it for the corridor guard.

The cat has no body, so its agent moves it by its position, along the way it planned: no wall holds it, and the way keeps it off them. A walker with a Character Body is moved by its velocity instead, so walls hold it too; chapter 6 gives the corridor guard one.

One mover a node

The cat still has the Path Follow that chapter 1 gave it. A warning triangle sits on its row in the Hierarchy, and the status bar says 1 problem. Click the 1 problem: the Problems tab opens on its row.

The same, with the Problems tab open in the bottom dock: All 1, Errors 0, and under Warnings 1 one row for cat reading This node has a Nav Agent and a Path Follow, and the Path Follow moves it after the agent every step, so the agent’s walk is undone — keep one of the two, or move the Path Follow to a parent or a child
Two movers on the cat, and the row the Problems tab gives them.

Each step the agent moves the cat towards the bowl, and then the Path Follow puts it back on the hearth loop. The cat would go round the hearth as before, and the agent’s walk would come to nothing. One of the two goes. Here it is the Path Follow.

  1. With cat selected, remove its PathFollowComponent with the bin icon at the right of its header. The row leaves the Problems tab, and the triangle leaves the cat’s row.
  2. Press Ctrl+S.

Its way, in Play

A walker’s way is drawn while the game runs, by the Routes gizmo: a line through the points of its way, a cross on its Target and a ring as wide as its Arrive Distance. It is on in Play unless you turn it off in the strip’s Gizmos menu.

  1. Press F5 to play in the editor. The cat sets off from the hearth.
  2. Walk the thief up into the hall, to the kitchen door, so that the view holds both the hearth and the bowl.
  3. Select cat. Press Bake on the strip, and in the strip’s Gizmos menu leave Routes and Navigation ticked and untick the rest.
Part of the Viewport in Play, the strip’s Gizmos menu open over the kitchen: All and None at its top; Collision, Triggers and Lights unticked, Navigation ticked, Paths, Forces, Framing, Markers, Audio and Shadows unticked, Routes ticked, Bodies and Signals unticked; Selected only and Hide while playing unticked; under it the strip reads Looking through game camera, Navigation baked, Bake, 273.3%, Gizmos (10 off); at the left the thief and the hall guard by the kitchen door, and a lime line through the door
The strip’s Gizmos menu after 60 steps, 1 s, of Play: Routes and Navigation ticked, the rest unticked.
Play, looking through the game camera at the hall and the kitchen: the hearth in the hall’s top wall, the cat below it with the move arrows on it, a lime line from the cat east a short way, then down to the kitchen door, through it, and across the kitchen to a cross and ring on the bowl; the thief by the kitchen door with the hall guard below her, the tables with their discs at the left; the Inspector shows the cat’s Nav Agent, Target 424.0 and 164.0, Moving ticked; the strip reads Navigation baked
The cat’s way after 60 steps, 1 s, of Play: through the kitchen door to the bowl. For this picture the thief was placed by the kitchen door from the start, where the step walks her.

The way runs through the kitchen door, the one opening between the hall and the kitchen. It is a line through cell middles, not smoothed: the cat walks it as drawn, and its last point is the Target itself.

Sent to the cellar

A Target can be typed while the game runs, and the walker plans again. Sent to the cellar, the cat has one way there: down the hole.

  1. Still in Play, with cat selected, type 88, 312 into Target, clicking each box once and pressing Enter: the middle of the cellar. If the cat has reached its bowl, Moving went off when it arrived; tick it again.
Play with the camera pulled back to the whole keep: the cat at the kitchen door, a lime line from it down across the kitchen into the corridor, east along the corridor’s carpet, up into the store room to the hole, and from the hole one straight lime line down to a cross and ring in the cellar; the Inspector shows Target 88.0 and 312.0, Moving ticked
With Target set to 88, 312 from the start of Play, after 300 steps, 5 s: the way to the hole, and the drop to the cellar in one straight line. The camera was pulled back for this picture.

The way goes through the kitchen, along the corridor and up into the store room, to the hole. There it crosses to the cellar in one line: the hole’s Teleport puts the cat there in one step, and it stops there. Now send it back.

  1. With the cat in the cellar, type 552, 120 into Target, the store room, and tick Moving. On the next step Moving goes off again, and no route is drawn: the hole goes one way, the hatch is shut, and there is no other way up. The cat stays where it is.
  2. Press F5 again to stop. What you typed in Play goes with it: the cat’s Target is 424, 164 again.
Play with the camera pulled back to the whole keep: the cat in the cellar with the move arrows on it and no lime line anywhere; the Inspector shows its Position -56.0 and 260.0, Target 552.0 and 120.0, Moving unticked
For this picture the cat was placed in the cellar and sent to 552, 120 from the start of Play. After 30 steps, half a second, Moving is off and there is no route. The camera was pulled back for this picture.

The cat’s Nav Agent

NodeComponentFieldValue
catNav AgentTarget424, 164
Movingticked
Speed30
Arrive Distance6
Face Walkticked
Path Followremoved

Every other field is as it comes: Use Links ticked.

Check it works