- Home /

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

**Answer** by koray1396
·
Nov 27, 2014 at 05:09 PM

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(2*gravity*MaxHeight)

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.

### Your answer

### Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.