Enemies stuck in corners

Get help using Construct 2

Post » Mon Jan 27, 2014 6:54 am

I really need some help here.
What is the standard way of avoiding that an enemy being chased by the player get stuck in corners?
I have a very simple, square layout. When the player get to less than certain distance, I
     set the angle of the enemy to angle(Player.X, Player.Y, Enemy.X, Enemy.Y)
     set enemy.speed = Player.speed + 10
Im sure this is an easy problem for you guys, but its driving me nuts.
Any help will be much appreciated
B
2
Posts: 9
Reputation: 176

Post » Mon Jan 27, 2014 7:06 am

I forgot to say that the enemy has Bullet behabiour
B
2
Posts: 9
Reputation: 176

Post » Mon Jan 27, 2014 8:29 am

There are cleverer ways but two simple improvements could be

when running away from player make the angle slightly random maybe

angle(player.x,player.y,enemy.x,enemy.y)+random(-45,45)

add a largish sprite (invisible) in the corner and when the enemy is over this then set the bullet angle away from the corner.

August 2015 - I misplaced a lot of links - I will try to find backups and repost. If You find a post that interests please reply to post with @rampackwobble and I will get a nudge!

If you find my posts useful please consider donating something to a local charity or to the http://www.mndassociation.org/
B
64
S
34
G
47
Posts: 1,194
Reputation: 30,892

Post » Mon Jan 27, 2014 9:02 am

I've tried those things, but what I get is that the 2 movements (i.e. running away from player and moving away from corner) start switching between each other and the sprite rotates back and forth, going nowhere.
What am I missing?
B
2
Posts: 9
Reputation: 176

Post » Mon Jan 27, 2014 9:05 am

what your missing is direction...what do you want the enemy to do chase, run or stay still, when player corners it?Pixel perfick2014-01-27 09:06:13
As long as I can move left, right and fire, I'm Happy...
B
42
S
15
G
11
Posts: 655
Reputation: 12,270

Post » Mon Jan 27, 2014 10:39 am

You need priorities and control tick.

First of all, put a timer on your enemy. This will be the control timer. Every time it runs out, the enemy should decide what to do : run from the player or escape corner. Then you call the corresponding function and start a new timer to choose a new action, but not right now.

What it would do in game :
T=0s : enemy choose to run from player, start new timer (2 seconds ?)
T=2s : enemy is not far from the corner, and the player is close too. The enemy chose to run from the corner, and starts a new timer. He will the run from the corner for at least the timer's duration, meaning it won't do back and forth every millisecond.

When you make AI decisions for anything, having a control timer is usefull to prevent micro-orders. I ran into the same problem with a "pet" simulation, where the pet would eat for less than a second, then start running, then eat again because its priorities were almost the same at that time, and each order changed the current priority in less than a second. If no order can be issued more than once in a while, this problem goes away. It makes the ennemy less reactive to the player's actions though.
Another solution is to have a control timer quite short (1 second ?) but in your decision function (the function that decides what the enemy should do), take into account what the enemy is currently doing. If he is running from the corner right now, maybe the proximity of the player doesn't have the same weight in the decision... It's all try and balance then.
B
17
S
8
G
4
Posts: 473
Reputation: 6,211

Post » Tue Jan 28, 2014 5:14 am

thanks a lot Guizmus, this should work I'll give it a try!tpd12014-01-28 05:15:34
B
2
Posts: 9
Reputation: 176

Post » Tue Jan 28, 2014 9:19 am

@Guizmus - Good point about timers, I will incorporate this idea in future.
August 2015 - I misplaced a lot of links - I will try to find backups and repost. If You find a post that interests please reply to post with @rampackwobble and I will get a nudge!

If you find my posts useful please consider donating something to a local charity or to the http://www.mndassociation.org/
B
64
S
34
G
47
Posts: 1,194
Reputation: 30,892


Return to How do I....?

Who is online

Users browsing this forum: brunopalermo and 14 guests