|
Setting a veiwing range for a AI character
I've been trying to make some kind of AI for a flash game, now I'm trying to make it attack when I get within a sertant range of it.
I got one problem, when I use the formula written below, I can get closer when closing up from behind than if I'm going from the front. This might sound like a good idea, but since the coordinates doesn't really change when the enemy turns around it's not good at all.
How can I stop this from happening?
-------------------
How I've done it now:
XG, XR, YG, YR and distance are all variables.
XG and YG are the _y and _x coordinates for a green circle.
XR and YR are the _y and _x coordinates for a red circle.
And distance is the range where it will react when you get close.
The "_root.gotoAndPlay("1")" is just something I've used to test if it does react when I get within that range.
if (Math.sqrt((XG-XR)*(XG-XR)+(YG-YR)*(YG-YR))<distance) {
_root.gotoAndPlay("1");
}
|