- Home /

# How to calculate the distance of an object with velocity?

Instead of calculating distance between 1 object and another, can I calculate the distance between an object and another, can I calculate the distance using the 'arch' an object with dynamics has (like a mortar firing)? Here's my example:

projectile trajectory:

http://en.wikipedia.org/wiki/Trajectory_of_a_projectile

good luck with that math! :) unforuntately I can't help you with that lol

**Answer** by alwayscodeangry
·
Jul 12, 2014 at 10:58 PM

Most of the trajectory equations can also be found here. I'm not 100% sure whether you're trying to get the horizontal distance or the arc length, so I've included both, and luckily `Mathf`

has all the functionality we need:

```
private static float GRAVITY = 9.8067f;
public static float GetHorizontalDistance(float velocity, float launchAngle, bool degrees = false)
{
// convert to radians if necessary
if (degrees)
{
launchAngle *= Mathf.Deg2Rad;
}
return (velocity * velocity * Mathf.Sin(2.0f * launchAngle)) / GRAVITY;
}
public static float GetArcLength(float velocity, float launchAngle, bool degrees = false)
{
// convert to radians if necessary
if (degrees)
{
launchAngle *= Mathf.Deg2Rad;
}
// pre-calculate sine and cosine of launchAngle and square of velocity
float sinAngle = Mathf.Sin(launchAngle);
float cosAngle = Mathf.Cos(launchAngle);
float vSquared = velocity * velocity;
return (vSquared * (sinAngle + cosAngle * Mathf.Log((1.0f + sinAngle) / cosAngle))) / GRAVITY;
}
```

You may need to swap out the const GRAVITY for Unity's value, depending on your usage case.

that is some angrycode!... :)

(couldn't help myself.. I just like your pseudo name)

### Your answer

### Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.