I am following Trajectory of a projectile formula. Does anyone know maths? - Unity Answers
My slightly modified code looks like this
float CalculateProjectileFiringSolution2(Vector3 start, Vector3 destination, float projectileSpeed) {
Debug.Log ("CalculateProjectileFiringSolution2. Start: " + start);
Debug.Log ("CalculateProjectileFiringSolution2. destination: " + destination);
Debug.Log ("CalculateProjectileFiringSolution2. projectileSpeed: " + projectileSpeed);
var y = destination.y - start.y;
var x = destination.x - start.x;
var v = projectileSpeed;
var g = Physics.gravity.y;
var sqrt = (v*v*v*v) - (g * (g * (x*x) + 2 * y * (v*v)));
if (sqrt < 0) {
Debug.Log("No Solution");
return 0;
}
sqrt = Mathf.Sqrt(sqrt);
var calculatedAnglePos = Mathf.Atan(((v*v) + sqrt) / (g*x));
var calculatedAngleNeg = Mathf.Atan(((v*v) - sqrt) / (g*x));
Debug.Log("angle: " + calculatedAnglePos * Mathf.Rad2Deg);
return calculatedAnglePos * Mathf.Rad2Deg;
}
When
start: (0,0,0)
destination (-1,0,0)
so that the destination point is on the left, i get 90 which is odd. Shouldn’t i get 180 considering (1,0,0) is 0 degree?
Anyway, let’s assume that is correct.
When
start: (0,0,0)
destination (1,0,0)
the angle comes out to -90.
when
start: (0,0,0)
destination (1,1,0)
i still get angle -90.
what am i doing wrong? Why do i get weird angles and why do i get -90 when i need to shoot at (1,0,0) and (1,1,0)?
projectileSpeed is very large so think of it as shooting straight with almost zero curve.
thanks