CharacterBody2D stops to follow NavigationAgentLine when reaching a wall's bottom corner

Godot Version

v4.2.2-stable

Question

Hello :slight_smile:

I have a problem with my pathfinder that I can’t figure out.

The enemy follows the palyer as expected but when it arrives to a bottom corner it stops following the NavigationAgent line and stuck in the wall

We can see the line is correct and the enemy should pass but it changes direction sudently every time it approches the corner. On the top corner, there is no problem

For the navigation area I’m using navigation layer on the tilemap and methods _use_tile_data_runtime_update and _tile_data_runtime_update to delete navigation layer when there is another tile with colider above. It looks like this when I launch the room scene directly (in normal conditions rooms are instancied/queue freed with code when passing a door. I guess there is a problem with the debog tool in this case, tiles still appears with navigation layer but they are clearly not according to the navigation agent line and the general behaviour of the enemy)

my enemy’s code is the following :

In the physics process :

nav_agent.target_position = get_tree().current_scene.player_node.global_position
dir = to_local(nav_agent.get_next_path_position()).normalized()
	
velocity = dir * actor.speed
var collision_info = move_and_collide(velocity*delta)
	if collision_info:
		if collision_info.get_collider().is_in_group("Environement"):
			velocity = velocity.bounce(collision_info.get_normal())

The part with bouncing is shared with other enemies, I tried to remove it and to change move_and_colide with move_and_slide but it doesn’t change the problem at all

The enemy’s node is simple, the physics collider is a bit small than the sprite. I tried to reduce it to 1px but still have the problem even if the enemy can pass the corner after touching it (and makes visual problem because the sprite enters into the wall)

The enemy’s node is CharacterBody2D with motion mode on Floating and min slide angle to 0°

If anyone have a clue on why this beaviour, it would be very helpful. Can’t understand why it stop sudenly to follow the Navigation agent line.

The agent path desired distance looks like it is set way too high as the agent can be seen skipping entire path corners way earlier. It just happens that it gets stuck on the physics collision on the bottom corner but the same can happen anywhere with that layout.

The TileMap build-in navigation has the major flaw that it has zero navmesh margin towards collision shapes. Try using a NavigationRegion2D in Godot 4.3 instead that can bake a better navmesh with some actual agent radius offset from collision and disable the inferior TileMap build-in navigation.

Thanks for your very fast answer.
I’ll take a deeper look to NavigationRegion2D because what I’ve quicly did make the behaviour really strange. The agent takes a very long way to reach the target and seems to stuck a bit on polygons outlines

I guess my multilayers tilemap and/or the fact the tilemap is the parent node of the scene (and the NavigationRegion2D is a child with geometry mode based on group) are in cause.

I’ll let you know if I can make it work

That detour is because you set the wrong postprocessing on the agent. The edge-centered forces all path points in the middle of the navigation mesh polygon edges. You need to switch back to default corridor-funnel postprocessing as that is the only thing that makes sense on a more optimized navmesh surface. The edge-centered is a crutch that was added for unoptimized navmesh layouts build around grids and tiles.

Indeed it’s better. But I probably miss something because it doesn’t solve the initial problem
As soon as the Agent is near the border, the body seems to not follow it anymore

As you can seen on this capture, the body is in the area where it shouldn’t, not following at all the line.

When it’s not on the border of the polygon, it’s ok

OK nevermind, I found the problem. The Path desired distance il the Agent was 10px, but 1px seems to solve the problem.

I’ll make more tests tomorrow but I guess it should be ok now. Tanks for your help !

The remaining issue is a matter of baking with enough agent radius margin for your physics movement and physics shapes.

Physics has highly unreliable update rates so it will have a hard time to not overshoot things which is what gets things stuck on collision or path points. Increasing both the bake agent radius and the agent desired distance by 1-2 extra pixels usually helps with this.

1 Like