Problems calculating the navigation agent's route

Godot Version

4.5

Question

(sorry if my english is bad, i speak spanish and my english is not good enough)

I have an enemy, who calculates his movement with a 2d navigation agent, this movement is managed with a state machine, which changes the state according to the distance between the player and the enemy. this is my enemy’s movement code:

extends EnemyStateBase

func _ready():
	call_deferred("manual_navigation")

func manual_navigation():
	await enemy.get_tree().physics_frame
	
	if is_instance_valid(player):
		navigation_agent_2d.target_position = player.global_position
	
func on_physics(_delta):
	verify_distance() //state_machine function which change the state
	manual_navigation()
	safe()
	
func safe():
	next_path = navigation_agent_2d.get_next_path_position()
	new_velocity = enemy.global_position.direction_to(next_path) * enemy.speed
	
	navigation_agent_2d.velocity = new_velocity

func _on_navigation_agent_2d_velocity_computed(safe_velocity):
	enemy.velocity = safe_velocity
	enemy.move_and_slide()
	
	if enemy.velocity.length() > 0:
		enemy.rotation = lerp_angle(enemy.rotation, enemy.velocity.angle(), 0.03)

The problem is a bit specific: it occurs when the enemy moves toward a specific point in my navigation zone.

My level’s navigation zone is generated from a tile map, which looks for “ground” tiles and tiles with collisions. This automatically generates the navigation polygon exactly the way I want it, but it has its flaws.

Here’s the problem, the enemy moves in an unwanted direction, deviating sharply from where it should move (which is the shortest distance towards the player)

This is what the section of my navigation region where the error occurs looks like.

Actually everything works fine, but I can’t allow errors like this to occur in such specific places.

I just watched some of this video talking about exactly this subject.
Maybe something there can help.

1 Like

i watch the video, I had already applied some parts to my project before, but doing what the video said I couldn’t achieve anything new.

I tried adjusting some navigation agent parameters, but nothing really seems to change. I don’t know if it’s the movement code or how I’ve set up the navigation agent. I think it has to do with the way pathfinding works in Godot.

Have you tried to see if you have the issue using Godot 4.4.1?

I believe I had the same issue with 4.5 here, and it was fixed with this PR. So if this is the same issue that I was having, you can: build the engine from the main branch (in order to get that fix), use 4.4.1 instead, or wait until 4.5.1 or 4.6 is released.

I’ve opted to use 4.4.1 until the next release.

2 Likes

@jd26 Have you solved it? I am having the same problem. I tried downloading 4.6 and 4.5.1 recommended by @foohines but I am still having the same problem

Well, I went back to 4.4.1 and everything seems to work fine, I guess they’ll fix it later in the newer versions.

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