0 votes

How do I make my enemy detect the player from a certain distance?
enter image description here

This is how my tree looks like
I originally tried using Raycast 2d to detect the player, but it detects not only the player, it also detects other things such as walls too.
I'd like some help on a way to do it in a more efficient way. If it matters, the game is a 2D platformer.
Thanks, if I wasn't clear enough ask me bellow and i'll try to explain it better.

asked Feb 12, 2018 in Engine by Decomius (15 points)

psuedo code

distance = enemy_position.distance_to(playerposition)
if distance < trigger_distance:
    do something

1 Answer

+1 vote
Best answer

Here's one method:

Attach an Area2D to your Enemy, and give it a circular collision shape with a radius of your desired detect distance. Then you can use the body_entered signal to detect an object entering that area and check if that object is the player.

To skip the object check, put your player on a specific collision layer, and set the area's mask to only check that layer.

answered Feb 12, 2018 by kidscancode (10,928 points)
selected Feb 13, 2018 by Decomius

Thanks, I'm gonna try this out as soon as I can =D

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.