GENIUS NEEDED, Calculating angles with given velocity and gravity acceleration problems.

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();
  • }
  • // 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!

You might want to take a look at this thread. How to calculate force needed to jump towards target point? - Unity Engine - Unity Discussions :slight_smile:

1 Like

THANK YOU SO MUCH!!! I have been fumbling with this for a while! THANK YOU :slight_smile: