Godot Version
v4.6.2.stable.official [71f334935]
Question
I’m making a rougelite game. I made a few simple enemies with attached NavigationAgent2Ds, using the following code.
func _on_detection_range_body_entered(body: Node2D) -> void:
if body is BaseEntity && body.teamNumber != teamNumber:
goal = body
navAgent.target_position = goal.global_position
navResetTimer.start(0.25)
func _on_detection_range_body_exited(body: Node2D):
if body == goal:
goal = null
navAgent.target_position = Vector2(0, 0)
navResetTimer.stop()
var isCloseEnough = false
func _physics_process(delta: float) -> void:
healthBar.value = HP
if goal is Node2D:
isCloseEnough = true if ((global_position.distance_to(goal.global_position)) < attackRange) else false
if goal != null && !isDead && anim.animation != "death" && !goal.isDead && !isCloseEnough && !isAttacking:
var nPathPos: Vector2 = navAgent.get_next_path_position()
var nPosDir = to_local(nPathPos).normalized()
velocity = nPosDir * (speed * 32) * delta
anim.flip_h = get_entity_dir(nPathPos)[1]
anim.play("walk" + get_entity_dir(nPathPos)[0])
elif !isAttacking && !isDead && anim.animation != "death":
anim.stop()
else:
velocity.x = move_toward(velocity.x, 0, speed)
velocity.y = move_toward(velocity.y, 0, speed)
if anim.animation == "death" || isAttacking:
velocity = Vector2(0, 0)
if isCloseEnough && goal != null:
velocity = Vector2(0, 0)
var prevPos = global_position
move_and_slide()
var newPos = global_position
if !isDead && newPos.is_equal_approx(prevPos) && !isAttacking && !anim.animation.begins_with("idle"):
var anim2 = anim.animation
if anim2.begins_with("walk"):
anim2 = anim2.right(-4)
if anim2.begins_with("atk"):
anim2 = anim2.right(-3)
anim.play("idle" + anim2)
print("idle" + anim2)
anim.stop()
func _on_navResetTimer_timeout():
if goal != null:
navResetTimer.start(0.25)
if navAgent.target_position != goal.global_position:
navAgent.target_position = goal.global_position
I added a navigation layer to a TileMap, but the paths I get from it are… very strange, to say the least. Am I doing something wrong? A picture of a path I get as well as the navigation layer are attached below.
Thanks for any help you can provide!

