- 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

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.

### Follow this Question

### Related Questions

real life:sin(2Pi)=0 unity Mathf.Sin(2Pi)=1.748456E-07 1 Answer

Distance from rigid body and drag 0 Answers

Enemy AI help with height check 1 Answer

Keep a certain distance between two RigidBodies 2 Answers

Car dynamics? 0 Answers