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.
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:
- Obstacles: setting it to custom means you can manually choose what can be an obstacle to your enemy through events.
- Range: radius of detection for the enemy.
- Cone of view: if your enemy has a front and back (not the case here), you can set it around 50 so the enemy can only see the player character if it’s facing it. Here, we set it to 360 so it can detect the player from any direction.
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.
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:
The action we’re going to add to the else statement is Add action → spriteEnemy → Set enabled → Enabled.
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:
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!