There are several ways to approach it. The easiest way is probably, before your enemy doing the chase do distance check. For example
extends RigidBody3D
const CHASE_DISTANCE = 10 # Adjust this value as needed
func _process(delta):
var distance_to_player = position.distance_to(player.position)
if distance_to_player < CHASE_DISTANCE:
# Add your chasing logic here
Another approach is to use an Area3D node to detect when the player enters a certain area around the enemy. Something along these line:
Create an Area3D Node:
Add an Area3D node to your enemy scene. This node will act as the detection zone for your enemy.
Adjust the Shape:
With the Area3D node selected, add CollisionShape3D, you can adjust the shape and size of the detection zone using the "Shape" property. For example, you can use a SphereShape to create a spherical detection zone.
Implement Detection Logic:
Add logic that can handle the detection of the player entering and exiting the detection zone. Here's an example script using GDScript to handle the player detection using an Area3D:
func _on_area_3d_body_entered(body: Node3D) -> void:
if body == player:
chasing = true
func _on_area_3d_body_exited(body: Node3D) -> void:
if body == player:
chasing = false
func _process(delta):
if chasing:
# Add your chasing logic here