How would i go about having an enemy patrol but still attacking?


So atm im working on a game and i want to have enemies that walk around and patrol an area, just on a path will do. Then i want them to be able to detect when a player comes into sight, but the player cant attack so he has to run away. Once the player runs away i want the enemy to go back to patroling the space.

Ive tried using emptys and edit object then go to them and then using the radar feature but once the player runs away the enemy just sits there doesnt go back to patroling.

any ideas?

Have you tried states?

You’re thinking of AI.
What you could do is use states, though im not really comfortable with those.
Either way a ‘state-like’ system is what you’ll need.
I suggest you have a script which defines functions.

to define a function use:

def func():
    #code here

You would need probable two states: attacking, and patrolling.
for attacking you would make the enemies run towards player.
they would shoot, and persue if neccessary
if the player runs outside a max distance;
change to patrol state


def attack:
attack stuff here

def patrol:
patrol stuff here

if getDistanceTo(Player,Enemy) > 10:

like that
(although it’s psuedo code obviously)

ok thanks guys, i havn’t really tried to mess around with programing yet, so ill try the states with logic first. I did try the states yesterday but i must have done something wrong, i had a feeling it was continuesly cycling through them, cause the enemy was just doing noting. Ill let you guys know how i go maybe post up a blend file when im as far as i can get with states.

birdAI.blend (150 KB)

Hey guys, ive gotten as far as i know how, it mostly seems to work except it wont collide, just gets close and stops.


pressing E will make you burrow underground so the bird cant detect you. ATM i have on collision it ends player but in the actual game it will stun lock the character so it cant move for 3 seconds.

So anyone wanna have a look and make some sugestions or fix’s so i can get it working :slight_smile: all much appreciated.

Try this, I added a motion actuator in state 2 so that in state 2 the bird will still move. Remember, unless you add states to each other, each state is like a separate layer of logic independent of the others.


birdATTACK!.blend (318 KB)

Thanks so much.

I cant believe i forgot to add the motion haha, silly me must be brain dead atm, i would normally know to add the motion :).