Advanced AI Help


I’m now moving onto sorting out the Advanced AI aspect of my platformer.

What I require is the ability for an AI to move in a certain position, back and forth. However, if a player comes within a certain distance then it follows the player. As soon as the player gets out of the distance, then the AI goes either back to his/her original spot or walks back and forth on the spot the player evaded them from.

Any ideas how to do this? Videos on tutorials aren’t really helpful to me in this regard. I spent 30 minutes following one just to have it not work.

If anyone could help with this part, it’d be much appreciated. I am a beginner so I apologize if this is a big ask. :slight_smile:


Hi, for the enemy movement you can take a look at the platformer tutorial, there is a part specifically dedicated to that: Platformer Tutorial, Part 6: Add Enemies to the Game [GDevelop wiki]
and there is this great example of SilverStreaks ‘Not a vania’ which includes several enemies with different behaviors, all of them very well documented GDevelop 5

1 Like

You need to create a variable for the enemy that will be responsible for the current state:

  • “patrol” - in this state, the enemy moves back and forth in the selected zone. This can be achieved by specifying the points between which he moves and, touching, changed direction to the opposite. It is also necessary to create a zone (for example, a circle), if a player enters this zone, then the state changes to “follow the player”;
  • “following the player” - the enemy moves after the player, respectively, while he is in the “radius of sight”. The state changes if the player left the observation area to “return”;
  • “return” - the enemy moves to the patrol zone. If it is reached, the state switches to “patrol”. In this case, if the player enters the visibility zone again, then it switches to the state of “following the player”.

This is the logic that is most often used in this case. For implementation details, see the tutorials above.

I gave a solution for this in another thread yesterday. It’ll do exactly as you’ve described.

I missed the platform bit :slightly_frowning_face:

I gave a solution for a top down version in another thread yesterday. You can use this as a starter.

1 Like

So you are using Patformer behavior. You need to create variables of Player just like @E1e5en said. After that, you need something like this:

If Player to Enemy distance is below 100px Then
Change the "follow player" Variable of Enemy to True

If Player to Enemy distance is below 100px (inverted) Then
Change the "follow player" Variable of Enemy to False

If the "follow player" Variable of Enemy is True Then
         If Player.X() < Enemy.X() Then
         Simulate Left keypress of Enemy

         If Player.X() > Enemy.X() Then
         Simulate Right Key Press of Enemy

You also need to set up JumpUP blocks. Something like this:
Here, the object I labelled 1 is JumpUP block and which I labelled 2 is your Platform. You need to keep these where you want your Enemy to jump. And after that:

If Variable "follow player" of Enemy is True and Enemy is in collision with JumpUP block Then
Simulate Jump Key for Player

This is a basic way of chasing the Player.

I’ve done the above.

However, on collision, the enemy Diego disappears.

What disappears? Enemy or Diego?

You also need to invert this condition

Here’s an explanation of a solution in another thread. If you combine that with my previous link, you should be able to get a reasonable chasing AI.

Very confused by his explanation. What is a node? Never heard that term in GDevelop and there’s also no simplistic definition of what he means by angles and x positions, etc.

Diego disappears. He follows the player within a certain area but then disappears once caught up to the player.

I have yet to add a part where Diego returns to his original position if out of the distance to player.

To be completely honest, him stopping when out of player boundary would be alright.

I think, realism wise, him returning to his original spot would be ideal though.

It’s like a step in the path.

The main take-away from that post is to put up some hidden pathfinding obstacles to stop the enemy from dropping off ladders and flying towards the player. So you’re effectively creating channels that the enemy can move along.

Oh okay.

I wish I understood that.

Unfortunately, I have no idea what pathfinding actually is. :sweat_smile:

Pathfinding is a behaviour you can attach to an object that you want to move (in this case your enemy obejct). It finds the best route from where the object is to a target position.

You also attach pathfinding obstacle behaviour to any objects that act as a barrier to the moving object. These will be objects that the moving object can’t go through (in your case, the platforms).

You then tell the object to move to a position, and it will move to that position while avoiding the obstacles.

I think these two events don’t let each other work.

When below 100 pixels, the variable is set to 1 and 0.

You are also changing the Position of Diego to Player.X() and Player.Y().

Are you sure that your Player is named PLAYER?

And how is your camera? Is it following the Player? Is it still and not moving?

Hey Mixen

I’ve changed the top one to be inverted as both of them would be clashing with each other without inverting one of them.

You are correct on spotting the Player bit. Something I skip over quite a bit. My main character is called ZombieBoy, not Player. After editing that bit, the collision does not delete Diego but instead moves him to in a center like position of the player. Not completely covering him but on top of him with his head at ZombieBoy’s stomach.

What do you think is a way to get rid of the collision effect causing Diego to be centered on ZombieBoy upon collision?

Oh wow. That’s handy.

Hey Mixen.

Just checking if you’ve read my message above. :slight_smile: (Forgot to tag you in it)