Path prediction

hi all~~
I am trying to create a turret behavior. What it does is that when a enemy attack object enters the range of turret,turret will turn and start shooting. I have the turning and facing the target the way I wanted. But the problem is that it is a machinegun like turret. And when
the turret turn and fires, it always misses by a little. I figure it is because instead of facing / looking at the current position of the target.
I should actually predicting the position of the target for the next second so my turret would fire in advance.

but I am not sure how to go about it.
Can anyone shed some light ?

Thanks

you must calculate the position of the target where it is when the bullet hits it (obviously, i know :wink: ). so calculate the time the bullet needs to travel to the target. then you take the normalized direction vector of the target and multiply it with targetspeed and traveltime.

float traveltime = Vector3.Distance(transform.position, target.position) / bulletspeed;
Vector3 movement = target.transform.foward * traveltime; // note: foward is already normalized, this assumes the target moves foward, change this if not
Vector3 predictionpoint = target.transform.position + movement; // this is the point to shoot at

code is untested but should give you a start.
normally this procedure is iterated until the difference between 2 runs is insignificant. when you run it and your prediction point is closer on the turret the traveltime is also shorter. but for practical usage one run should be enough unless you have very slow projectiles. this method also does not take the current rotation of the target into account. feel free to add it when you need ;).