View Full Version : [AS3] Making Enemy AI Rotate To Face Player
Ciubhran
03-10-2009, 06:52 PM
Problem solved. Another problem is issued in the third post of this thread regarding AI and collision detection.
runawayprisoner
03-10-2009, 07:00 PM
Do an enterFrame or setInterval and put the methods for rotating the AI in there?
setInterval is more desirable because you can control how often you rotate your AI (you wouldn't want to do that every time the frame updates because that's a large hit to performance).
For more details (sample codes, maybe), you should state what you're working with. AS 2.0 or AS 3.0?
Ciubhran
03-10-2009, 08:32 PM
In the topic it clearly states [AS3] :)
However, this problem is solved, but I have another problem that is a bit more troubling.
The enemies now face and run towards the player (2d-view game seen from above), but if i try to "round them up", by just running around them, they eventually stack on eachother, and this is an affect I do not want.
Anyone that can give me any tips to counter "stacking"?
And dont just say "collision detection"; I want concrete examples. If I add collision detection to stop the units, they will eventually just end up stopping eachother when they collide and will never move.
runawayprisoner
03-10-2009, 09:39 PM
Why stop them when you detect a collision?
if(enemy1.hitTestObject(enemy2)) enemy2.direction = -enemy2.direction;
Make them "bump" into each other instead.
And sorry, didn't see the thread title before. I haven't had much sleep lately, and then there were all these projects that got piled up.
ayjayuk
06-05-2009, 10:59 AM
Hi
Im having exactly the same issue with following enemys "stacking" on top of each other.
the "bumping" method causes the enemy to run off briefly in the other direction which isnt the effect i need either. :)
I'm imagining we are both looking for the same collision solution, which can do hittests for all enemys in an array so whilst they are travelling towards the player they are also jostling for position in order to surround the player without "stacking" or runnning around in different directions.
Ive got a basic demo showing how far ive got, including aggro radius and stuff like that, but im kinda stuck wanting to FIX this stacking issue...
http://www.thehalt.co.uk/flAS3/nmeCome7.html
would really appreciate any help comments with this, its holding me back from implementing loads of other cool stuff :)
thanks in advance
andi
How are you currently moving them? Do they have xSpeed and ySpeed?
ayjayuk
06-05-2009, 02:58 PM
Im using a foreach command to cycle the array of enemies. Each Enemy (upto a max of 6 at a time) gets assigned a point to head towards (the little dots around the player).
I use the code below to get them to rotate towards the player but head for the dots. the idea for the dots originallly came from trying to stop them all overlapping in exactly the same place... but it aint working :P
Any help would be much appreciated, even if it means scrapping this method, for something a bit smarter :)
P = the player,
nme = the enemy
function moveTo(nme:*,target:*){
if (nme.aggro >= 100 && nme.follow == true && nme.melee == false){
nmeRadians = Math.atan2(p.y - nme.y, p.x - nme.x);
nmeDegrees = Math.round((nmeRadians*180/Math.PI));
nme.rotation = nmeDegrees+90;
yChange=Math.round(target.y - nme.y );
xChange=Math.round(target.x - nme.x );
yMove=Math.round(yChange/speedCut);
xMove=Math.round(xChange/speedCut);
nme.y+= yMove;
nme.x+= xMove;
}else {
//stay still
}
if (nme.melee == true){
nme.follow = false;
}
}
|
vBulletin® v3.8.5, Copyright ©2000-2012, Jelsoft Enterprises Ltd.