Hello, its my first time. And im having problems.
My enemy follows me all good and dandy but sometimes it just doesnt and makes suicide.
As you can see the enemy paths are on point but at some point they break free from path and go in rando direction and does not follow target.
enemy.gd
extends CharacterBody3D
class_name Enemy
const SPEED = 5.0
const JUMP_VELOCITY = 4.5
@export var target: Node3D = null
@export var player_node:CharacterBody3D
@onready var area_3d: Area3D = $Area3D
@onready var navigation_agent_3d: NavigationAgent3D = $NavigationAgent3D
var follow_target: bool = false
var timer: Timer
func _ready() -> void:
area_3d.body_entered.connect(on_area_entered)
add_child(timer)
target = null
pass
func _physics_process(delta: float) -> void:
if player_node:
if player_node.enemy_follow:
follow_target = true
else:
follow_target = false
if target and target is Player and follow_target:
navigation_agent_3d.target_position = target.global_position
var direction = (navigation_agent_3d.get_next_path_position() - global_position).normalized()
if direction:
velocity.x = direction.x * SPEED
velocity.z = direction.z * SPEED
rotation.y=lerp_angle(rotation.y,atan2(-velocity.x,-velocity.z),0.2)
else:
velocity.x = move_toward(velocity.x, 0, SPEED)
velocity.z = move_toward(velocity.z, 0, SPEED)
# Add the gravity.
if not is_on_floor():
velocity += get_gravity() * delta
move_and_slide()
func on_area_entered(body):
print("enter: ",body)
target = body
pass
I have made a function that will flip the boolean player_node.enemy_follow
when i press the "E" key
And i have attached the the player node to every enemy instance exported value.
World navigation
Enemy agent