Godot Version
4.2.2
Question
Good morning fellow Godot devs!
I’ve implemented pathfinding for the very first time and I have encountered an unexpected behaviour.
The PathfinderAgent2D having scene is supposed to follow the player around and avoid some obstacles that are within the tilemap. These obstacles have the physics layer drawn on it and no navigation layer. All the ‘allowed’ tiles have the opposite situation.
Afterwards I bake the NavigationPolygon within the NavigationRegion2D node in the level scene and it looks like this:
But when I run the scene, the enemy will look for a direct path to the player, avoiding the obstacles but very often choosing a path that is outside of the blue bounds of the NavigationPolygon and get way too close to the obstacle ultimately getting him stuck.
Here’s what I mean:
Good decision:
Bad decision:
On the second printscreen he is ignoring the bounds of the NavigationPolygon. He swaps often between these two kind of paths, and sometimes gets stuck like so:
HELPFUL INFORMATION:
Enemy code:
class_name Enemy extends Entity
@onready var navigation_agent_2d : NavigationAgent2D = $NavigationAgent2D
@onready var update_pathfinder_timer : Timer = $UpdatePathfinderTimer
var player : Player
var path_to_player : Vector2
func _ready():
player = get_tree().get_first_node_in_group("player")
func _physics_process(delta):
current_direction = path_to_player
resting(delta)
move()
func get_path_to_player() -> Vector2:
navigation_agent_2d.target_position = player.global_position
var next_path_position = navigation_agent_2d.get_next_path_position()
var direction = global_position.direction_to(next_path_position)
return direction
func _on_update_pathfinder_timer_timeout():
path_to_player = get_path_to_player()
Here are the NavigationRegion2D inspector settings:
Here are the NavigationAgent2D inspector settings:
Any ideas on why he’s bouncing between good and path pathfinding decisions? Thanks in advance!