0 votes

how to create a small ia for an enemy, which has to follow the player when he detects it

And seen in videos that you can use a raycast but if so what would be the code

in Engine by (91 points)

1 Answer

+1 vote

Raycast is not likely to help you much here. There are several possible approaches, with the simplest probably being this:

1) Put an Area2D on the enemy and connect its entered/exited signals.

2) When the player is inside this detect area, move towards it. To move towards something, you find the vector pointing to the target:

var dir = (target.position - position).normalized()

If you'd like to see a more detailed example of this, see here:

Godot Recipes: Chasing the player

by (20,677 points)
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.