[EXAMPLE] 2D platformer stealth game, IA for enemies

In this example project:

  • The player can hide behind walls.
  • Enemies will attack the player only if they are aware of their presence.
  • Enemies become aware of the player when they see them or when the player touches them.
  • Enemies follow a set path but can deviate from it to chase the player.
  • The project includes some basic tricks to make enemies return to their path after chasing the player.
  • The player takes damage during a collision with the enemy only if the enemy was previously aware of the player’s presence.
  • If the player quickly overtakes an enemy that was unaware of their presence, that enemy will appear confused for a moment and then attack the player (this is an unintended lucky effect).

This example was born as a response to this other topic but is designed to be highly modular and optimized for scalability. It may not be very beginner-friendly, but if you have any questions or want to try adapting it to your game, I’ll do my best to help you if I have the time.



Thank you, colleague, for a useful example. It was interesting to study! I found something new for myself)

1 Like

Hello @erdo ,
thanks for the assets.
I really do not understand how the raycast reconize player from ground.

you create a raycast to a group. In the group there are players and ground.
In the second line you check a boolean variable. I suppose that if raycast “meet” the player, the variable is set as true but I don’t understand where you set it.
Where do you set “is_player” as true ?

1 Like

Hi! At the beginning of the scene I create a boolean object variable called “is_player” with the value “true” for the player object, this happens just once:
… then, from the position of each enemy, I scan with a raycast towards the objects contained in the group “visible_to_the_enemy”, if an enemy finds an object that happens to have a variable “is_player” with a value “true”, then such enemy is seeing the player:

Thank you,
now I understand.
the second line is called only once. So if the player is behind a wall the is_player variable is not populated.
Thank you very much,

All gdevelopers that arrive in this post can also go deep about raycast argument with this example:


1 Like

It’s more like the is_player object variable is just populated once at the beginning of the scene for the player object; then, if the raycast hits an object with such variable (and the value “true” in it) it’s the player object.

Thanks a lot @erdo .
Now all is clear !

1 Like