0 Favourites

Enemies stuck in corners

  • 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

    I?m sure this is an easy problem for you guys, but it?s driving me nuts.

    Any help will be much appreciated

  • I forgot to say that the enemy has Bullet behabiour

  • 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.

  • 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?

  • what your missing is direction...what do you want the enemy to do chase, run or stay still, when player corners it?

  • Construct 3

    Buy Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Buy Now Construct 3 users don't see these ads
  • 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.

  • thanks a lot Guizmus, this should work I'll give it a try!

  • Guizmus - Good point about timers, I will incorporate this idea in future.

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)
Similar Topics Posts Views Last Post
Topic
24 1,759
eli0s's avatar
eli0s
Unread hot topic
0 Favourites
Enemy Tank AI
36 3,594
Neo1000's avatar
Neo1000
Unread hot topic
28 1,167
JLH1964's avatar
JLH1964