Hello all -
I am making a simple turret system that fires at a target as it flies by. I currently have the code set up so that the turret tracks the target, and fires at it whenever it detects a "hit."
The problem is, that when the target is moving in a straight line or something like that, my bullets will never actually hit the target. The turret fires, but the moment it fires and the projectiles are on their way, the target has already moved to a new position. Therefore unless the target turns in toward the turret or away or some such action, the projectiles will simply miss. As the target moves in a circle around the turret, the bullets will never hit. Ever. Thats just the nature of the beast :)
However, I want to add some sort of leading on the turret so that it can hit the target. Now, I know with random movement my odds of hitting it are the same whether I shoot at where it is or if I lead the target. However, even with random movement as I am going to be using it, there are still going to be situations where the target will be going in a straight line long enough for projectiles to hit it if they are aimed properly.
So my question is, what is the best way to "lead" my target so that providing there are no additional forces acting on the object, my bullets will hit?
My current idea, though I don't know how to implement this in code, is :
Have some sort of variable (float, maybe vector3), speed, that gives me the speed and direction of the target.
Then, have the turret take into account on any given frame the speed of the target, the distance of the target from the turret, and the projectile speed. With that information, I should be able to solve for how much "lead" space there needs to be. Then just take the current position of the target, add the "lead" space ahead of it where it is moving to, and fire there. In theory, if the target is then not acted upon by any forces, the bullets should hit. Any ideas on how this could be implemented?