NavigationAgent2D does not NavigationRegion2D in vertex

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:

nav-problem

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

My problem got me thinking of a workaround: adding multiple NavigationRegion2D and connecting them together.

Unfortunately for me this does not fix the problem, the agents will refuse to cross one NavigationRegion2D to get to the other.

See the screenshot below: the enemies (NavigationAgent2D) refuse to cross the boundaries of the NavigationRegion2D:

Not the purple circles denoting that the two NavigationRegion2D are linked.

This lets me to believe that the problems are somehow related: the agents will not cross bounds no matter what.

Curious.

I’ve figured it out, the NavigationAgent2D was accidentally not a child of the CharacterBody2D but a peer / sibling. This causes the behaviour as seen in the gif and image.

So if anyone has this problem in the future make sure the NavigationAgent2D is a child of the CharacterBody2D!

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.