How do I create basic AI?

Get help using Construct 2

Post » Fri Dec 26, 2014 2:08 am

Just simple enemies that wander in a single direction, and change their direction appropriately when they collide with a solid. I'm trying to make a simple bomberman clone, and surprisingly this has been the most challenging part to think of. Every attempt I have ever made, the enemy always ends up going through walls, and it's quite frustrating. I've been trying to substitute "on collision" with "is overlapping" combined with triggered once, as I've heard that 'on collision' can be cpu intensive.

pls help! :|
B
11
S
2
Posts: 91
Reputation: 1,017

Post » Fri Dec 26, 2014 2:32 am

create an instance var for enemies called AI (number-type)

When AI = 0 and every 2 secs,
-> Set AI to choose(1,2)

When AI = 1
-> Simulate enemy "Right"
-> Set enemy to walk animation
-> Set enemy Not Mirrored

When AI = 2
-> Simulate enemy "Left"
-> Set enemy to walk animation
-> Set enemy Mirrored

Enemy has a wall to right
-> Simulate enemy "Left"
-> Set enemy to walk animation
-> Set enemy Mirrored

Enemy has a wall to left
-> Simulate enemy "Right"
-> Set enemy to walk animation
-> Set enemy Not Mirrored

That's a basic AI. You can set AI = 3 for run right, 4 for run left, 5 for attack, 6 for hurt, etc. if you want more complex AI

For run and attack, I suggest using a var to store every tick the distance between you and every enemy so each enemy will calculate its own unique distance from you, then use that distance variable to determine when enemy will attack (for example X<50 when close range attack, x>150 when long range attack). Differentiate Run from Walk by the speed (adjust both max speed and vector x for both run and walk). Of course animations of Run and Walk should be different, or at least different in animation speed.

Oh I forgot, you have to use "For Each: Enemy" on every single event of AI to make each behave independently from other enemy instances.

I hope this gets you started on AI. Good luck.
B
16
S
7
Posts: 301
Reputation: 2,726

Post » Fri Dec 26, 2014 4:27 am

Your method seems to be based on 2D platformers, I probably should have specified it's a top down game
B
11
S
2
Posts: 91
Reputation: 1,017

Post » Fri Dec 26, 2014 8:57 am

Basilboy wrote:Your method seems to be based on 2D platformers, I probably should have specified it's a top down game


Oh, my bad. I am not familiar with the bomberman game. But if it's a top down game, you can pin another object that is longer (horizontally wider than your enemy, means higher X). It should be set to invisible and will act as sensor. So when it overlaps with your wall, check which wall (left or right) and change direction accordingly.

Btw, even if your game is top down, you can still use the same logic I used in my earlier post. Use an instance var called AI and assign a var value if moving left and another if moving right. Change the var when sensor overlaps with wall.

Hope it helps.
B
16
S
7
Posts: 301
Reputation: 2,726

Post » Fri Dec 26, 2014 10:56 am

You probably should check out this tutorial I made on how to make a bomberman game.

There are patrolling enemies that should be exactly what you are looking for, and everything is adapted to what you seem to want.
New to Construct ? Where to start

Image Image

Image Image

Please attach a capx to any help request or bug report !
Moderator
B
294
S
117
G
96
Posts: 7,311
Reputation: 71,086


Return to How do I....?

Who is online

Users browsing this forum: dop2000 and 23 guests