- Edited
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