ForumsProgramming ForumEnemy AI

6 3009
Gfitz07
offline
Gfitz07
203 posts
Nomad

I have searched many weeks on the internet trying to find something that would help me with enemy AI (artificial intelligence) for a top view game.

How would you make a enemy go after your character when he is a certain distance away, and attack if he gets close enough?

I would really appreciate it if someone could help me.

  • 6 Replies
Gfitz07
offline
Gfitz07
203 posts
Nomad

AS2 sorry

LordBob
offline
LordBob
517 posts
Nomad

in what language?

Gfitz07
offline
Gfitz07
203 posts
Nomad

Action Script 2.0

LordBob
offline
LordBob
517 posts
Nomad

sorry, not familiar with action script.

Programpro
offline
Programpro
562 posts
Nomad

Kk, to point the enemy at the player the code is:

if (playerx > enemyx) {
enemy._rotation = Math.atan((playery -enemyy) / (playerx - enemyx)) * (180 / Math.PI);
} else {
enemy._rotation = 180-(Math.atan(( playery - enemyy) / (playerx - enemyx)) * (180 / Math.PI));
}


Definition of variables:
enemyy = enemy's y position
enemyx = enemy's x position
playery = player's y position
playerx = player's x position
enemy._rotation = enemy's rotation


To make make the enemy move in the direction in which you pointed it the code is:


enemyx += Math.cos(enemy._rotation*(Math.PI / 180)) * 10
enemyy += Math.sin(enemy._rotation*(Math.PI / 180)) * 10


Finally, to make the enemy attack when within range, the code is

var dist = Math.sqrt(Math.pow(enemyx-playerx,2)+Math.pow(enemyy-playery,2))

if (dist < attackdistance) {

enemy.attack();

}


Definitions:

attackdistance - constant value...how close the enemy and player need to be to cause attack
enemy.attack(); - some function you make that makes the enemy shoot.

Gfitz07
offline
Gfitz07
203 posts
Nomad

Thanks a lot!

Showing 1-6 of 6