Godot Version
4.5
Question
(sorry if my english is bad, i speak spanish and my english is not good enough)
I have an enemy, who calculates his movement with a 2d navigation agent, this movement is managed with a state machine, which changes the state according to the distance between the player and the enemy. this is my enemy’s movement code:
extends EnemyStateBase
func _ready():
call_deferred("manual_navigation")
func manual_navigation():
await enemy.get_tree().physics_frame
if is_instance_valid(player):
navigation_agent_2d.target_position = player.global_position
func on_physics(_delta):
verify_distance() //state_machine function which change the state
manual_navigation()
safe()
func safe():
next_path = navigation_agent_2d.get_next_path_position()
new_velocity = enemy.global_position.direction_to(next_path) * enemy.speed
navigation_agent_2d.velocity = new_velocity
func _on_navigation_agent_2d_velocity_computed(safe_velocity):
enemy.velocity = safe_velocity
enemy.move_and_slide()
if enemy.velocity.length() > 0:
enemy.rotation = lerp_angle(enemy.rotation, enemy.velocity.angle(), 0.03)
The problem is a bit specific: it occurs when the enemy moves toward a specific point in my navigation zone.
My level’s navigation zone is generated from a tile map, which looks for “ground” tiles and tiles with collisions. This automatically generates the navigation polygon exactly the way I want it, but it has its flaws.
Here’s the problem, the enemy moves in an unwanted direction, deviating sharply from where it should move (which is the shortest distance towards the player)
This is what the section of my navigation region where the error occurs looks like.
Actually everything works fine, but I can’t allow errors like this to occur in such specific places.

