How to make a enemy start chasing you only after you get into a certain radius?
-
So, I am making a simple 3rd person game and I made enemies chase me. But I want them to chase only when I get near them. How would I do that?
-
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 anArea3D
node to your enemy scene. This node will act as the detection zone for your enemy. -
Adjust the Shape:
With theArea3D
node selected, addCollisionShape3D
, you can adjust the shape and size of the detection zone using the "Shape" property. For example, you can use aSphereShape
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 anArea3D
:
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
-