artificial intelligence for an enemy

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

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

:bust_in_silhouette: Reply From: kidscancode

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