Here's a little trick I tried today that works great and replaces tons of math-heavy events. Some of you probably have done this yourselves and might say "well, duh", but I thought I'd share it since it had baffled me up until now.
I wanted to make my AI-controlled ship be able to track me, but not turn instantly. A similar functionality was to make tracking missiles. I also use this to let the AI know when it's facing its target so it knows when to fire, etc.
Originally I did all this crap with 8 events per object checking its direction and the relationship between X & Y coordinates of the target, etc. But then I discovered "look at" recently and had started using that, but the problem was that it was an instant change in direction.
So here's what I've done instead. I just made an invisible active object called "AIAimBot" and anything that wants to aim at a target does the following steps:
1) Set Position of the AIAimBot to the shooter's/projectile's current position.
2) Make the AIAimBot look at the target (could be an object or a position, whatever you want)
3) Set AIAimBot's Alterable Value D to AIAimBot's current direction minus the shooter's projectile's current direction. This gives you the difference between the desired direction and your current direction.
4) Because of the way that direction numbers go from 0 to 31, you can end up with odd numbers here (like -17 or 28) that would make you turn in the wrong direction. So we need to "normalize" it - that is, make sure the difference in direction (the "delta") is never more than 16 or less than -16 (in other words, no more than a 180 degree turn). To do this, all you have to do is add 32 to AIAimBot's "D" value if it's less than -16 or *add* -32 to it if it's over 16. This will make sure your value is between -16 and 16.
5) Now you know how far off your current direction the direction to your target is. You can use this for multiple things. For my AI and tracking missiles, I use it to check "FOV" (field of view). It the number is < -8 or > 8, then I know the target is more than 45 degrees off to one side. At this angle, my tracking missiles lose their lock and stop tracking. I also use this, though, to determine which direction to turn in if I *do* want to turn toward the target. It's as simple as this: if AIAimBot's "D" value is 0, I'm right on, but if it's > 0, I need to Set Direction to my current direction plus some number (I add 1 to make slow, smooth turns), and if it's < 0, then I need to subtract from my current direction. Usually I do this once every 10/100ths of a second so that the turning speed is at a constant rate but not to jerky.
Another nice optional thing you can add on top of this is some random direction tweaks to make "drunken" tracking missiles. All you have to do is, once you've done the above, set up a timed interval event to add Random(3)-2 to the current direction of your projectile. This will still track but have some randomness to the tracking - kinda makes a sort of anime-ish drunken tracking missile behavior.
One nice thing about this is that, since events are processed in linear order, you only need one AIAimBot for all of your objects - just make sure that when and object is going to use it, it repositions it, makes it look and calculates and normalizes the "D" value first.
Hope this is helpful!



Reply With Quote



