Find angle needed to shoot a moving target?

If i know the velocity of a moving target’s rigidbody, how fast a bullet fired from a position moves, and the distance between the target and firing position, how do i find the angle to fire that will cause the bullet and target to meet?

trig and AI predictions! bake out the collage math book!
I would search for this. it’s been done a hundred times.

1 Like

Have fun

Think of the “distance” between target and firing position as displacement, which is a vector. We’ll call the displacement vector X, which has units of distance. Since the velocity V of the target is also a vector, and has units of distance over time, the displacement of the target over time T is V * T, in the direction of the velocity. Adding these two vectors together, you get the position where you want your bullet to be. Let’s call this position Y.

Now, the distance between position Y and the origin, or shooter position, is the magnitude of the displacement. This would be Vector3.sqrMagnitude, respectively. Divide this distance by the speed for time T, which is the same as time T in our last argument. Now, you have two equations with the same T, which is a key variable in your problem. Sorry if you wanted a more code-like answer.

Edit: edited last paragraph.

aight, just refreshed my SOHCAHTOA knowledge. Would CAH, the cosine of theta equaling the adjacent divided by the hypotenuse work?

Left out the most important part – Cos(theta) = (vec1 dot vec2) / (magnitude of vec1 * vec2)

Thanks, i’ll definitely study your previous answer and try it out the code.