Enemy leading target

Hi forum.

So I am trying to learn how to create an enemy object capable of leading its target. Previously, I have used the steering actuator set to seek behavior to have enemies aim at or follow a player character. But simply moving in a straight line is enough to avoid any projectiles. How can the enemy turret or bot lead its target, taking into account the target’s movement, distance, and a given travel time for its bullet?

I am always looking out for super simple solutions using logic bricks or short python scripts because coding is beyond me, but I understand that this is a complex problem. :spin: Has this been done before in BGE? Any help is appreciated.

object.worldPosition +( object.worldLinearVelocity*multiplier)

will give you a point ahead of your object.

I use this for my own version of low cost Continuous Collision Detection by casting a ray ahead of fast moving objects :slight_smile:

object.worldPosition +( object.worldLinearVelocity*multiplier)

hmm didnt realize the math was that simple…good to have…//bookmarked//

there is probably better math, to return exactly where the thing will be in x frames

but This is what I ran into that works **

Sorry but I am noobier than that. I don’t know how to implement this.

Would this go into a steering or edit object actuator, or would I need to use it in a python script of its own?

here is an example :smiley:


leadOff.blend (501 KB)

If anyone is interested in a more complex method of leading a target here’s a function I’ve used for years to do the job. Can’t remember where I found the original code, but I converted it to python and got this:

def lead_target(target_position,target_velocity,bullet_position,bullet_velocity):
    error = 10
    max_error = 0.002
    tp2 = [0, 0, 0]
    dt = 0.0

    bp = bullet_position

    while True:
        tp2 = target_position + target_velocity * dt
        r = bullet_velocity *dt
        error = (tp2[0]-bp[0])**2+(tp2[1]-bp[1])**2+(tp2[2]-bp[2])**2 - r**2
        if error < max_error or dt > 100.0:
            a = tp2
            return a
            dt += 0.1

I’m not sure I’d write it the same way again:

while True

is potential trouble maker… but it does the job very well.

I think this is what I need! Thank you for your patience!