# Trajectory of a projectile formula: get the same angles

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);
}
``````

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

Try this one which I’m posting you will get exact trajectory of projectile. The following method is for 2D physics you can easily modify for 3D if needed. Here you need to pass start position of projectile and initial velocity and method will return the number of points on the projectile trajectory.

``````Vector3 [] GetTrajectoryPoints(Vector3 pStartPosition , Vector3 pVelocity )
{
float velocity = Mathf.Sqrt((pVelocity.x * pVelocity.x) + (pVelocity.y * pVelocity.y));
float angle = Mathf.Rad2Deg*(Mathf.Atan2(pVelocity.y , pVelocity.x));
float fTime = 0;
Vector3 [] pos = new Vector3;
int iNumberOfPoints = 10;

fTime += 0.1f;
for (int i = 0 ; i < iNumberOfPoints ; i++)
{
float x = velocity * fTime * Mathf.Cos(angle * Mathf.Deg2Rad);
float y = velocity * fTime * Mathf.Sin(angle * Mathf.Deg2Rad) - (Physics2D.gravity.magnitude * fTime * fTime / 2.0f);
pos *= new Vector3(pStartPosition.x + x , pStartPosition.y + y ,0);*
``````
• `````` 	fTime += 0.1f;*
``````
• `````` }*
``````
• `````` return pos;*
``````
• }*