# Math Genius NEEDED! Algorithm problem with parabola

Hi all, I am trying to make a targeting system with an artillery turret in which there is a set distance, velocity, and gravity acceleration. This targeting system is supposed to calculate the angle in which the turret should fire at to hit the target. I am having some trouble as it is either undershooting or overshooting the target, and am wondering if I have the ratios of real world values like Unity velocity to Real World velocity mixed up or if the equation is wrong. Here is my code:

`````` public class test : MonoBehaviour {
public float angle;
public float g = Physics.gravity.y;
public float v = 1f;
public float d;
public Transform firePoint;
public Transform target;
public TestAAT3 obj;
// Use this for initialization
void Start () {
obj = FindObjectOfType<TestAAT3>();
}

// Update is called once per frame
void Update () {
if (Input.GetKeyDown(KeyCode.Alpha1))
{
CalculateAngle();
}
}

//d = distance between firepoint and target
//g = gravitational acceleration
//v = given velocity
//angle = theta
public void CalculateAngle()
{
d = Vector3.Distance(firePoint.position, target.position);

angle = (1/2) * Mathf.Asin((g * d) / (Mathf.Pow(v_, 2)));
Debug.Log(d);
obj.Fire(d);
}
}
``````

Any and ALL help is appreciated. I’ve looked at wikipedia, khan academy videos, etc. Nothing helps!

Well first of all “g” should be the value of the acceleration. This should be positive. There is no direction information in a single value. You seem to use the y component of the Physics gravity vector which is usually negative.

Next thing is the formula you use only works when both your firePoint as well as your target are on the exact same height. Also keep in mind when you rely on Unity’s physics engine for the actual simulation that this assumes no drag at all.

You also need to check if the term inside ASin is not lower than “-1” or greater than “1”, In that case the target can’t be reached at all. Also ASin would return a NaN in that case.

If the height of the two points can be different you have to use this one instead. Of course here a similar check has to be done. You can only reach the target when the root has a real value (i.e. the value inside the root has to be 0 or positive). If the root is a complex root the target can’t be reached,

Finally what do you actually pass to your “Fire” method? Currently you pass the distance between you and the target. Shouldn’t you somehow pass the angle?

ps: Never use “pow” for simple powers. This `(Mathf.Pow(v_, 2))` should be simply
`(v_ * v_)`