Site icon

Construct 3 Basics #3: Enemy AI and groups

In most games, one of the most important elements can be the enemies, trying to prevent the player from reaching their goal. Enemy AIs vary in complexity, depending on the type of game you want to make. In this tutorial, we will focus on a very simple base enemy AI you can use in your Construct 3 game, and maybe modify it later to suit your needs, while also talking about how you can keep your project’s coding organized by using groups.

If you haven’t followed the previous tutorials, don’t worry, this tutorial can still be useful to you.

1 Once again, let’s open the project we created in the first tutorial through Menu Project Open recent. If you haven’t downloaded the assets I mentioned in the previous tutorial yet, no problem, just download it here. Once you extract the file, let’s right-click on the layout and create a new object as we always do: Insert new object Sprite and name it “spriteEnemy”.

Click on the folder icon and select the “spriteEnemy1.png” file from the asset folder you downloaded.

Then, click on the collision polygon icon and make sure the object’s collision box looks like what’s shown below and close the editor window.

2 Now that we have created our enemy object, place it on this spot on the layout:

Now, add the Platform, the LineOfSight, and the Sine behaviors to the enemy object.

Then modify the properties of those behaviors to this:

To clarify the meaning of the properties under the LineOfSight behavior:

3 Now, if you test the game you’ll see our enemy object is already moving thanks to the Sine behavior, but we still need to make the code to make the enemy chase the player, so it’s time to visit our Event Sheet 1 again. If you’re making a big project, it’s in your best interest to keep your code organized, and that’s where the groups come in. 

Right-click on the blank space and select Add → Add group.

Then, name it “Enemy” and click OK.

You’re now going to right-click on the group we just created, go to Add → Add sub-group.

Name it “Enemy line of sight” and click OK. Right-click that sub-group and go to Add → Add sub-event.

Select the “spriteEnemy” object and click on Has LOS to object.

Select the “spriteCharacter” object and click done.

In this event, click on Add action and go to “spriteEnemy”  Sine → Set enabled, choose Disabled and click on Done.

4 Right-click on the event we just created, go to Add → Add sub-event → spriteEnemy → Compare X.

Set it up like this and click on Done:

Inside this sub-event, go to Add action → spriteEnemy → Platform → Simulate control.

Set it up like this and click on Done:

What this event is doing is testing if the player is within the enemy’s line of sight, and if they are it tests if they are on the left side of the enemy (if the enemy’s X is greater than the player’s X). If that’s true, the Platform behavior makes the enemy chase the player to the left of the screen. Now we need another sub-event to test the right side.

Just copy and paste the last sub-event. Change the parameter of this new event’s condition to less than, and change the action’s parameter to pressing Right. Your event sheet should be looking like this:

5 Right-click on the main event under the Enemy line of sight group, and go to Add → Else.

The action we’re going to add to the else statement is Add action → spriteEnemy → Set enabled → Enabled.

6 Next, we’ll need an event to “kill” the player once they collide with the enemy. Right-click on the Enemy main group and add a sub-group called Enemy hit. We’re going to add an event to this sub-group by clicking on Add event to ‘Enemy hit’, located right below the sub-group.

Then click on spriteEnemy and then on Is overlapping another object.

Choose the spriteCharacter object and click on Done.

Now that we have the condition, go to Add action → System → Restart layout.

So every time the enemy collides with the player, the current layout restarts and you have to try again. Your groups should be looking like this:

7 Let’s test it!

And there you have it! Of course, an enemy can behave in many different ways, this is just a very basic example if you’re just starting in this engine. Next time, we’re going to elaborate more on the game over aspect, once the enemy reaches the player, and we’re going to make a “victory” screen for when the player gets to the end of the level.

I hope this was useful to you and until next time!

Did you enjoy this article? Then consider buying me a coffee! Your support will help me cover site expenses and pay writers to create new blog content.

Exit mobile version