Hello.

I am having a really hard time getting my head around making a convincing pathfinding system in 2D.

I have followed a whole bunch of tutorials but am just hitting roadblock after roadblock. Either my enemies get super twitchy, or they get stuck, and they only avoid each other and not static bodies or collision shapes on the map.

I was wondering how people normally implement this kind of a system or if there are any good resources (paid or otherwise cos god knows I have looked).

For context the game I am trying to make will have lots of enemies moving together through a loose kind of maze where they will converge on an enemy base at the other end (similar to creeps in DoTA for example).

I would like them to crowd round the base in a circle at the end (but I am less worried about that for now and more concerned about just getting normal collision avoidance (as the shape of the maze will be changing dynamically and I would prefer to not have to add Navigation Polygons over and over in code in real time.

func _ready():
	
	update_navigation_target(player.global_position)
	
func _physics_process(delta) -> void:
	
	
	update_navigation_target(player.global_position)
	
	current_location = global_position
	next_location = nav_agent.get_next_path_position()
	path_direction = current_location.direction_to(next_location)
	new_velocity = path_direction * speed
	nav_agent.set_velocity(new_velocity * delta)
	
	
##### NAV FUNCTIONS
func update_navigation_target(player_location: Vector2) -> void:
	nav_agent.set_target_position(player_location)
	
func _on_navigation_agent_2d_target_reached() -> void:
	print("target_reached")
	
func _on_navigation_agent_2d_velocity_computed(safe_velocity) -> void:
	velocity = velocity.move_toward(safe_velocity, 0.25)
	move_and_collide(velocity)

Here is the basic code I am using (and I have avoidance turned on for the NavigationAgent2Ds.

Can anyone help me understand what I am doing wrong and how to make them avoid collisions with each other and also with objects on the map (ie. trees/towers).

Or alternatively just point me in the direction of some good resources.

I have been over the docs time and time again and I am really struggling to understand the finer points of the navigation2D. For example does the radius of a navigationAgent2D even do anything?

Any help greatly appreciated.

<3

    8 days later

    Hotcrossbun

    As the tooltip or class documentation can tell you the NavigationAgent agent_radius is for avoidance calculations. It does not affect your pathfinding nor does it affect your path movement that you do in your custom script. All it does is affect the avoidance velocity calculations.

    Physics collision shapes are not valid avoidance objects nor do they matter for pathfinding. They simply do not exist for the avoidance nor for the pathfinding. They only exist for physics and the PhysicsServer. Hence why your pathfinding and avoidance agents ignore them and get stuck on them. The only way to tell the pathfinding about your physics collision shapes is through your NavigationPolygons.

    There is no way around changing your NavigationPolygon if you change your maze dynamically. The NavigationPolygon is what tells pathfinding that it can traverse those areas. If you keep a NavigatioPolygon in an area that is already blocked by maze physics collision all you get is your agents stuck on collision. For the pathfinding that will be still a valid path to go as long as you keep that NavigationPolygon there. A NavigationPolygon describes the traversable safe area for an agents center. You always need to shrink a NavigationPolygon according to your agents size else they will again get stuck on collision if the NavigationPolygon edge and the collision shape edge overlap cause there is not enough room left for your agent.