Problems with NavigationAgent3D

Godot Version

4.2.2

Question

Hey there! I want to make that my test_enemy patrol the maze. To do that i use NavigationRegion3D and NavigationAgent3D. NavigationAgent3D is child of test_enemy that have script:

extends CharacterBody3D

@onready var agent = $NavigationAgent3D
var SPEED = 3
var targ : Vector3
signal get_new_target


func _ready():
    pass

func _physics_process(delta):
    look_at(agent.get_next_path_position())
    rotation.x = 0
    rotation.z = 0

if position.distance_to(targ) > 0.5:
	var curLoc = global_transform.origin
	var nextLoc = agent.get_next_path_position()
	var newVel = (nextLoc - curLoc).normalized() * SPEED
	velocity = newVel
	move_and_slide()
else:
	emit_signal("get_new_target")

func updateTargetLocation(target):
agent.set_target_position(target)

I cant understand why my test_enemy always banging its head against the wall before he gets the point.

Link to the video: https://www.youtube.com/watch?v=3XGLTYavRPQ

As you can see in the video, navigation mesh is also located on the walls. I would be grateful if you could tell me how to fix it (If, of course, it can cause any problems and is considered incorrect).

That just looks like you have not enough margin for your physics collision shape size added.

So increase the agent_radius in the NavigationMesh properties or reduce the size of your collision shape.

Also make sure that you add extra margin for your agent desired distances. When you have a lot of path points around sharp corners the agent will slightly leave the navigation mesh because the new path point could be already in reach and so gets skipped.

Since you have a lot of navigation mesh baked on top your maze you should use the baking AABB of the NavigationMesh to limit what gets baked. The small navigation mesh pieces on top are bad for performance should they end up as the closest target position for a path for whatever reason.

1 Like

Thanks