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.

in Engine by (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.

by (20,381 points)
selected by

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.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.