- Home /

# Addforce to go a specific distance and height

I have a rigidbody sphere with mass 1 and use gravity set to true i wanted to add force to the sphere so that it goes distance d and height h (a parabolic path)

Is there an equation to find add force vector ?

will the starting speed of the sphere always be 0? is this in 3D space or from a 2D point of view? and if its in 3D in which direction would you define distance (displacement really as distance doesn't have direction!)

Thanks, Scribe

this is a purely general math or game question

this site is only for very specific questions about Unity

go here

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

or ask on stackoverflow, or gamedev, for many answers instantly

yes starting speed always 0

the game is in 3D space but 2D view (orthographic)

I am using x (distance) y (height)

**Answer** by aldonaletto
·
Jan 21, 2013 at 12:26 PM

This is the so-called "ballistic trajectory": an initial velocity is given to the projectile at certain elevation angle, and physics does the rest. The mass doesn't matter, only the direction and magnitude of the initial velocity, thus it's better to calculate the velocity vector and assign it directly to **rigidbody.velocity**.

You can split the velocity into two components, vertical and horizontal. The vertical speed is "consumed" linearly by the gravity, and the maximum height is reached when it becomes zero. This allows us to calculate the initial vertical speed:

```
vertSpeed = Mathf.Sqrt(2 * g * maxHeight);
```

The horizontal velocity doesn't change during the trajectory (we're not taking air resistance into account), thus the distance can be calculated by:

```
horSpeed = maxDistance / totalTime;
```

The total time depends on the height of the target: if it's at the same height as the launching point, the total time will be equal to twice the time taken to reach the max height:

```
totalTime = 2 * vertSpeed / g;
```

If in your case the vertical and horizontal directions are the axes Y and X, you can calculate the rigidbody velocity as follows:

```
var g = Physics.gravity.magnitude; // get the gravity value
var vSpeed = Mathf.Sqrt(2 * g * maxHeight); // calculate the vertical speed
var totalTime = 2 * vertSpeed / g; // calculate the total time
var hSpeed = maxDistance / totalTime; // calculate the horizontal speed
rigidbody.velocity = Vector3(hSpeed, vSpeed, 0); // launch the projectile!
```

Thanks! This help me a ton in my 2d game since I needed "bounce" the player to a specific height. In case it is of any help to anyone, with a variation on the vSpeed formula I used:

```
float velocityY = Mathf.Sqrt(
2f * -Physics2D.gravity.y *
rigidbody2D.gravityScale * verticalDistance);
rigidbody2D.velocity = new Vector2(0, velocityY);
```

**Answer** by wijesijp
·
Jan 22, 2013 at 04:43 AM

Aldo:

I think i got some idea why my projectile is not working. When I use your equation for a single rigidbody it is working well.

What i have is 3 rigid bodies connected together with HingeJoints. When I apply your equation to one of the links it is moving very short distance.

What should I do in a situation like this?

well I have some experience with force

it will never stop except draw slows them down and the mass of the other 2 components HingeJoints might be applied to the 1 with the script with force

I probably don't understand totally how your project goes and probably most of my info isn't true to the fact I don't understand exactly what's happening in your project

i guess other 2 objects are slowing the object where i apply the velocity. And the object does not reach set height or distance.

i guess if we can calculate the velocity for full object and applied to 1st link in chain it might do the trick.

### 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.