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.
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
else:
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.