# How to calculate the angle of a trajectory to hit the target

Hi everyone,

How do I calculate the angle of a trajectory to hit the target, without knowing the velocity.
I only know the max height, offset height and the distance to the target.

This is what I got so far (I don’t know how to calculate offset height ):

``````float GetAngle(Vector3 startLocation, Vector3 endLocation, float maxHeight)
{
float distance = Mathf.Sqrt(Mathf.Pow(startLocation.x - endLocation.x,2) + Mathf.Pow(startLocation.z - endLocation.z,2));

float offsetHeight = endLocation.y - startLocation.y;
//how do I calculate offset height in this equation ?
return -Mathf.Atan (4 * maxHeight/ distance ) + Mathf.PI;
}
``````

I use this to calculate the velocity (works fine I only need the correct angle):

``````float LaunchVelocity (Vector3 startLocation, Vector3 endLocation, float angle)
{
float range = Mathf.Sqrt(Mathf.Pow(startLocation.x - endLocation.x,2) + Mathf.Pow(startLocation.z - endLocation.z,2));
float offsetHeight = endLocation.y - startLocation.y;
float gravity = Physics.gravity.y;

float velocity = range * range * gravity;
velocity /= range * Mathf.Sin(2 * angle) + 2 * offsetHeight * Mathf.Pow(Mathf.Cos(angle),2);
return Mathf.Sqrt(velocity);
}
``````

first, you should need to complete your code, otherwise you will always face similar problems.

I think there is no need to deal with complex formulas, I find simple steps helpful;

• VerticalSpeed = Mathf.Sqrt(2gravityMaxHeight)
• TravelTime = Sqrt(2*(MaxHeight - OffsetHeight) / g) + Sqrt(2*MaxHeight / g)
• HorizontalSpeed = Distance / TravelTime

So your speed vector would be VerticalSpeed + HorizontalSpeed.

You can calculate the angle by Mathf.Atan2. Speed would be Magnitude of FinalVector.