NavigationPolygon and NavigationAgent

Godot Version



In this game I am making, I want the enemies to chase the player around the walls and obstacles. The enemies use a NavigationAgent2D, and the scene has a TileMap and a NavigationRegion2D.

I added a navigation layer to the TileMap so the enemies properly pathfind around the TileMap walls to get to the player

For testing purposes, I manually excluded the Hole from the NavigationPolygon. My ultimate goal is to exclude the different obstacles with code. I saw an example in the Godot 4.0 documentation here
I am using 4.2, and even if the make_polygons_from_outlines function is now deprecated, I’m guessing it is still possible to do.

This is the code that the enemies use to move
var direction = nav_agent.get_next_path_position() - global_position
direction = direction.normalized()
velocity = direction * SPEED

I’m creating this topic because I’m having a hard time debugging the weird paths that are created for the enemies when they are chasing the player.

I haven’t figured out why the enemies keep going straight through the hole even thought it is not part of the NavigationPolygon

And sometimes, the enemies will have this very weird path around TileMap walls.

Of course the enemy has collision with that wall so it doesn’t work, but the path is still created that way, and then it often changes to a good path around. FYI, my code sets the nav_agent.target_position to player.global_position every .5 seconds if the enemy is in chasing mode. This might be why some paths are weird?

Here are a few more screenshots to help

how about the navigation target?

these all inside _physics_process right

Not inside the physics_process no, the enemy has a timer and every .5 seconds this function gets called to update the navigation agent’s target with the player’s position

ok, but the movement is still inside physics process block

var direction = nav_agent.get_next_path_position() - global_position

Disable the TileMap build-in navigation when you use a NavigationRegion2D to bake the navigation mesh. If you do not do this the TileMap will spawn region cells at runtime creating conflict with the navigation mesh of the NavigationRegion2D.

1 Like

That did it! Thank you so much
Is there a way to combine the TileMap navigation and the NavigationRegion2D?
I really liked the simplicity of the TileMap navigation, not having to worry about re-drawing the polygons after I mess around with the TileMap

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