So my main problem is that even though the pathfinding code for my enemy AI makes the enemy avoid each other when avoidance is enabled, the enemy will try to avoid the player even though th enemy is supposed to be chasing the player. It makes the enemy move around the player as if they're strafing when they get too close to the player but, there's not animation for that. Other times, the enemy would just run on the spot when they get tooo close to the player. Here's the code for the enemy pathfinding:
func cone_of_vision(delta, sweep_amount = 100,cone_range = 45):
if (parent.ray.rotation_degrees.y >= -cone_range) and (parent.ray.rotation_degrees.y < cone_range):
parent.ray.rotation_degrees.y += sweep_amount * delta
else:
parent.ray.rotation_degrees.y = -cone_range
func follow_path(delta: float, move_speed: float):
var next_location = parent.nav_agent.get_next_path_position()
var current_location = parent.global_transform.origin
# Horizontal steering only
var desired = (next_location - current_location).normalized() * move_speed
desired.y = 0.0
# Smooth steering on XZ plane
var current_h = Vector3(parent.velocity.x, 0, parent.velocity.z)
var target_h = current_h.move_toward(Vector3(desired.x, 0, desired.z), 8 * delta)
# Tell the NavigationAgent3D what velocity we *want*
parent.nav_agent.set_velocity(target_h)
# Read the last computed (avoidance-adjusted) safe velocity from the agent (fallback to target_h if not ready)
var adjusted_velocity = parent._nav_safe_velocity if ("_nav_safe_velocity" in parent) and parent._nav_safe_velocity.length() > 0.0001 else target_h
# Apply it
parent.velocity.x = adjusted_velocity.x
parent.velocity.z = adjusted_velocity.z
# Let gravity_force() handle velocity.y
parent.move_and_slide()
# Smooth rotation instead of snapping
if adjusted_velocity.length() > 0.1:
var target_dir = Vector3(adjusted_velocity.x, 0, adjusted_velocity.z).normalized()
var current_dir = -parent.global_transform.basis.z
var lerped_dir = current_dir.lerp(target_dir, 6 * delta).normalized()
parent.look_at(parent.global_position + lerped_dir, Vector3.UP)
I'm using the velocity computed signal from the NavigationAgent3d; Avoidance is on. So it's relevant for me to also show you this:
func _on_nav_velocity_computed(safe_velocity: Vector3) -> void:
_nav_safe_velocity = safe_velocity
``
Can anyone spot the problem here? I was following this tutorial that thought me about avoidance but, I notice that the problem I'm seeing isn't happening when the enemies reach close to the player. The section of the video that addresses Collision Avoidance is timestamped.
https://www.youtube.com/watch?v=-juhGgA076E