Problems with NavigationAgent3D

Godot Version



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():

func _physics_process(delta):
    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

func updateTargetLocation(target):

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

Link to the video:

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