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