¿How to make enemy chase player if detected?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Arielrandal

Hello, sorry for this dumb question, but how can i make an enemy detect the player if it enters his area and then follow the player? Both player and enemy are kinematicbodys3d if it helps. Thanks for any answer.

:bust_in_silhouette: Reply From: Wakatta

By no means a dumb question infact wish there was someone to ask when I was trying to do it.

So it boils down to the type of game and the approach you want to use STATE_MACHINE or EVENT_MECHANICS

But I’ll give a quick example here

Connect the body_entered, body_exited signals of the area node and enable/disable movement code

var follow_player = false

func _on_body_entered(body):
    if body == player_node:
        follow_player = true
func _on_body_exited(body):
    if body == player_node:
        follow_player = false
func _process(delta):
    if follow_player:
        #movement code here

Thanks for the help, it is really hard for me to learn, do you know how could i print the collider name? just to test a few things

Arielrandal | 2021-06-10 17:23

On the topic of learning, Just go at your own pace.

Something that works really well to get results from mammalia is positive reinforcement.
So every time you get that eureka moment or complete a task you’ve set out, reward yourself.

how could i print the collider name?

#for area nodes
func _on_body_entered(body):
    print(body.name)
#for raycast nodes
func foo():
    print(get_collider().name)

Wakatta | 2021-06-10 20:28