# Target tracking with a small delay (actionscript 3.0)

I'm having trouble thinking of a good method to track my character with an enemy attack. Of course, I don't want the attack to track my character's current position; I want it to track where the character was about 1 second before (so you can move around and make the attack miss and loop around you sort of a thing).

The general structure of my game uses a timer to update all of my events. I have a timer going off every 25 milliseconds that updates everything, including my player's position and the enemies position. Right now I just have the enemy attack directly targeting my character....which works fine except that it's impossible to escape =p.

Let me know if I didn't supply enough details. My approach was going to basically be get my character's position from about 1 second ago, then have the enemy target that position, the only problem is I can't think of a good way to get my character's position from previous times. Thanks for the help!

• Have you tried reducing the rate at which the enemy can move its targeting position? – UnderscoreZero Sep 23 '13 at 21:03
• Yes I have but I don't like the outcome. It doesn't have the "looping effect" that i'm looking for. It just kind of slowly homes in on you instead of quickly rushing past you – John Dodson Sep 23 '13 at 21:16
• Oh, it's a missile? That's a whole different story. There's quite a few questions related to missile style projectiles. But most of them are trying to home in on the target rather than miss it. but it might still be useful info: gamedev.stackexchange.com/questions/17313/… – UnderscoreZero Sep 23 '13 at 21:21
• Oh ok, interesting post...yeah I'm afraid it's a little more complicated than I initially thought =p – John Dodson Sep 23 '13 at 21:29

One simplistic approach can be by making the targeting fuzzy. Instead of directly targeting a location, choose a random location within a given radius range. Ideally you are projecting a point you are targeting based on velocity and heading of both your missile and the target, and then applying the fuzzieness.

Having a minimum radius on that fuzziness can decrease the maximum accuracy. So that random location is never on target directly if you don't want it to be.

You could then also shrink how fuzzy that is the closer you get just by shrinking the min/max radius, so that you are not missing the target based on fuzzieness.

Obviously the more fuzzieness you have, the more likely that missile is going to miss that target.

The simplest thing to try: Make the rate that the missile turns very low while the rate that it moves forward is very high. That way it can't make quick turns and will zoom past a dodging target.

Another approach that may look better: Track only until the missile is within a certain distance of the target, and then the missile is locked onto it's course until it has passed the target position. That way it won't correct again until after zooming past.

I am answering exactly this question:

I want it to track where the character was about 1 second before

Solution #1, simple.

Your requirement may be rephrased as "aiming position must reach actual terget position within 1 second". When missile just spawned, set aiming position same as actual target position. On every update, lerp aiming position to target position, using dt/lag as lerp factor. Here dt is time since previous update and lag is desired delay. Alternatively, when missile just spawned, set its aim bit towards its current flight direction, so it will not instantly turn to target.

Solution #2, precise.

On every update save pair of current time and target position into FIFO-queue, but also remove from that queue previous items thar are older than 1 second. This way you got movement history for your entity. Use current record in queue as aiming position.

Solution #3.

There is defect in previous solutions: fast moving missile may jump ahead its aim. I can imagine few ways for solving this, but I want to show simplest. Take solution n1 as base, and instead of lerping aim towards target, just set aim as lerp between missile position and target position with same factor dt/lag.

missile::update(dt)
{
lf = dt / me.desiredLagTime;
me.aimPos = me.target.pos * lf + me.pos * (1.0 - lf)
}