← PathwayLesson 5 · Enemies & Simple AINext lesson →

Lesson 05 · 70 minutes · Challenge layer

Make an enemy choose.

Build a guard that patrols while the player is far away, chases after detection, deals damage on contact, and returns to patrol when the player escapes.

STATESAREA3DNAVIGATIONCHASEDAMAGE
Big ideaEnemy AI is a set of named states connected by visible conditions.
PredictLeaving the detection area should return the enemy to patrol, not leave it stuck chasing.
TestTrigger each state deliberately and narrate what condition caused the transition.
Why it mattersReadable challenge gives players a chance to learn, choose, and recover.
01 / MODEL

State machine

One enemy, three clear modes.

Patrol

Move between points and watch for the player.

Chase

Update a navigation target and pursue while detected.

Return

Navigate back to the patrol route after the player escapes.

Transition

A detection signal changes the current state; the state controls behavior.

States prevent one large function from trying to patrol, chase, attack, and return at the same time.

02 / NAV

Scene setup

Give the guard a walkable map.

enemy.tscnEnemy · CharacterBody3DNavigationAgent3DDetectionArea · Area3DCollisionShape3DMesh + Body Collision
  1. Add NavigationRegion3D to Main.Create a NavigationMesh resource and bake the floor.
  2. Create the enemy scene.Add its mesh, physical collision, detection area, and NavigationAgent3D.
  3. Create two patrol markers.Add Marker3D nodes to Main, place them on the baked region, then assign both to the enemy’s patrol_points array.
  4. Assign groups and signals.Keep Player in player; connect DetectionArea’s body_entered and body_exited.
  5. Test the map.Turn on visible navigation in the Debug menu and confirm the route stays on the baked floor.
Godot's 3D viewport toolbar with a NavigationRegion3D selected, showing the Bake NavMesh button.
Select the region, then bake. With NavigationRegion3D selected, its toolbar grows a Bake NavMesh button — click it after assigning a NavigationMesh resource.
Godot's 3D viewport showing a teal-highlighted baked navigation mesh hovering over a floor plane.
The teal overlay is the walkable map. That highlighted surface is what NavigationAgent3D actually paths across — if your floor isn't covered, the enemy can't reach it.

Screenshots: Godot Engine documentation, © Juan Linietsky, Ariel Manzur and the Godot community, CC BY 3.0.

03 / CODE

Chase behavior

Ask navigation for the next safe position.

enemy.gd
extends CharacterBody3D
enum State { PATROL, CHASE, RETURN }
@export var speed := 3.5
@export var patrol_points: Array[Node3D]
@onready var agent: NavigationAgent3D = $NavigationAgent3D
var state := State.PATROL
var player: Node3D
var patrol_index := 0

func _ready() -> void:
    if not patrol_points.is_empty():
        agent.target_position = patrol_points[0].global_position

func _physics_process(_delta: float) -> void:
    match state:
        State.PATROL: patrol()
        State.CHASE: chase()
        State.RETURN: return_to_patrol()
    follow_path()

func patrol() -> void:
    if agent.is_navigation_finished() and not patrol_points.is_empty():
        patrol_index = (patrol_index + 1) % patrol_points.size()
        agent.target_position = patrol_points[patrol_index].global_position

func chase() -> void:
    if is_instance_valid(player):
        agent.target_position = player.global_position

func return_to_patrol() -> void:
    if patrol_points.is_empty(): return
    agent.target_position = patrol_points[patrol_index].global_position
    if agent.is_navigation_finished(): state = State.PATROL

func follow_path() -> void:
    if agent.is_navigation_finished():
        velocity = Vector3.ZERO
        return
    var next := agent.get_next_path_position()
    var direction := global_position.direction_to(next)
    velocity = direction * speed
    move_and_slide()

func _on_detection_body_entered(body: Node3D) -> void:
    if body.is_in_group("player"):
        player = body
        state = State.CHASE

func _on_detection_body_exited(body: Node3D) -> void:
    if body == player:
        player = null
        state = State.RETURN
Navigation is not collision. Navigation finds a route; CharacterBody3D and CollisionShape3D still handle physical contact.
The player leaves detection. Which state should run next?

04 / DAMAGE

Fair challenge

Signal danger before punishment.

Add a red detection glow or sound before the chase begins. Use a short invulnerability timer after damage so one collision cannot remove all health in a single second.

player.gd · damage cooldown
var can_take_damage := true

func take_damage(amount: int) -> void:
    if not can_take_damage: return
    can_take_damage = false
    health -= amount
    if health <= 0: respawn()
    await get_tree().create_timer(1.0).timeout
    can_take_damage = true

Remix challenge

Add an INVESTIGATE state. When the player escapes, the enemy first walks to the last position where it saw the player.

Duplicate the project as SignalRun_05_AI.Your restore point must demonstrate Patrol → Chase → Return → Patrol and a one-second damage cooldown.
05 / CHECK

Definition of done

Your enemy is fair when…

0/4 checked

Evidence: Narrate the current state while a partner deliberately triggers every transition.

Lesson 5 complete

Your game now creates pressure and meaningful risk.