Godot Version
4.5
Question
Hello,
I’ve created a level which contains a NavigationRegion2D inside of the region I’ve added a square polygon and baked the NavigationRegion2D.
I’ve added enemies which have a NavigationAgent2D, that have use Avoidance enabled with the following code:
extends Node2D
@export var speed := 100.0
@onready var character_body = $CharacterBody2D;
@onready var navigation_agent = $NavigationAgent2D;
var PATH_FINDING_RETARGET_AFTER_SECONDS := 0.2
var path_finding_retarget_time := Time.get_unix_time_from_system()
func _ready():
Global.player_detected.connect(_on_alert_raised)
func _physics_process(_delta: float):
if navigation_agent.is_navigation_finished():
return
# Limit the amount if times a path is calculated to the player.
var now = Time.get_unix_time_from_system()
if now > path_finding_retarget_time:
# target the player
navigation_agent.target_position = Global.player.character_body.global_position
path_finding_retarget_time = now + PATH_FINDING_RETARGET_AFTER_SECONDS
var next = navigation_agent.get_next_path_position()
var new_velocity = character_body.global_position.direction_to(next) * speed
navigation_agent.velocity = new_velocity
func _on_player_seen(_body_rid, _body, _body_shape_index, _local_shape_index):
Global.player_detected.emit()
func _on_alert_raised():
# target the player
navigation_agent.target_position = Global.player.character_body.global_position
func _on_navigation_agent_2d_velocity_computed(safe_velocity: Vector2):
character_body.velocity = safe_velocity
character_body.global_rotation = lerp_angle(character_body.global_rotation, safe_velocity.angle(), 0.1)
character_body.move_and_slide()
I’m seeing behaviour which I think I should not see: the enemies refuse to cross the area’s / vertexes (not sure what the proper name is) of the NavigationRegion2D, as shown in this gif:

When the player is in the colored area of the enemy the pathfinding works. Note that this is all one baked NavigationRegion2D with one NavigationPolygon.
I’m a beginner in Godot and I’m probably doing something wrong, but I’m not sure what.
Does anyone have a tip for me?
Thanks in advance!
MrHus
