# Calculate initial velocity for projectile to reach point at a given angle and calculate angle to reach point at a given velocity.

So I would like to have a turret that launches projectile in an arc at the player. To do this I need to calculate the velocity and angle at which to launch the projectile in order to hit the player.
The method I have attempted so far involves first calculating the velocity needed to reach a given point at a given angle. To do this I solved for v in the equation, Δy = (Sinθ·v)(Δx/Cosθ·v) + (0.5·g)(Δx/Cosθ·v)^2 My solution was the following equation.

x and y are respectively the horizontal and vertical distances between the turret and the target and G is equal to gravity. I used this equation to calculate the initial velocity needed to fire a projectile to the the edge of the turret’s range when launched at a 45 degree angle. The following is the code used.

``````//_launchAngle = 45 degrees
_initialVelocity = Mathf.Sqrt((0.5f * -Physics2D.gravity.y * Mathf.Pow(_range, 2)) / (Mathf.Pow(Mathf.Cos(_launchAngle * Mathf.Deg2Rad), 2) * (0 - Mathf.Tan(_launchAngle * Mathf.Deg2Rad))));
``````

After the initial velocity is calculated I use it in the following equation to calculate the angle to launch a projectile at in order to hit a given point(The player/target location).

v is the initial velocity calculated at the beginning, g is gravity, and x & y are once again the horizontal and vertical distances to the target.

The issue is that the second equation always come out as NaN. From my research I am fairly confident that the equation is the correct one to use in this scenario. I have have attempted setting the points to their absolute values, and using negative gravity, however neither changed the results. I have tried different equations for velocity and they did not seem to work either. I assume I am doing something wrong, or am missing something, but I am at a loss for what that could be.