- Home /

# Calculating force

I have two points: A launch point, and a target. I have a script that calculates the force i need to apply to my object, so it lands on my target.

Formula where v0 is initial velocity, R is the range, g is the acceleration of gravity, and θ is the angle.

This works fine, but only if the y-axis of my launch point and target are the same. How can i calculate the force i need, but also being able to hit my target, even if it is above or below me.

Code:`using UnityEngine; using System.Collections;

public class LobbTest : MonoBehaviour {

```
public int roundTo;
public Transform target;
private Rigidbody rb;
private float range;
private float gravity;
private float angle;
private float velocity;
private float angleRad;
public void Lobb ()
{
rb = GetComponent<Rigidbody> ();
range = target.position.x - transform.position.x;
Debug.Log ("range = " + range);
gravity = -Physics.gravity.y;
Debug.Log ("gravity = " + gravity);
angle = transform.eulerAngles.z;
Debug.Log ("angle = " + angle);
velocity = (Mathf.Sqrt ((range * gravity) / CalculateSine(angle, 2)));
Debug.Log ("velocty = " + velocity);
rb.velocity = transform.right * velocity;
}
float CalculateSine(float angle, float multiplier)
{
float radAngle = angle * Mathf.PI / 180f;
float radSine = Mathf.Sin (multiplier * radAngle);
radSine = Round (radSine, roundTo);
return radSine;
}
float Round(float f, int roundTo)
{
return Mathf.Round (f * Mathf.Pow(10, roundTo)) / Mathf.Pow(10, roundTo);
}`
```

}

I really doubt there is someone mathematically skilled around here. You'll be better off trying on your own :D

so the launch position and the launch target are fixed, as well as the launch angle, and you need to calculate the launch speed ?

here's my approach.

the key is that you know the formula for the X & Y position of the projectile over time as a function of the initial velocity, and the initial velocity (X & Y) is a function of the angle and the speed. You can use all that to work backwards to get the speed.

**Answer** by elenzil
·
Mar 18, 2016 at 05:46 AM

yes. slightly different derivation than above, but:

```
// speed = +/- sqrt( GX^2 / (2C * (SX - CY)) )
// where
// X, Y = location of target
// C, S = cosine & sine of theta
// G = gravity
float speed = float.NaN;
float C2_times_SX_minus_CY = 2f * C * ((S * X) - (C * Y));
float G_times_X_SQUARED = G * X * X;
if (Mathf.Approximately(C2_times_SX_minus_CY, 0f)) {
// divide by zero
}
else if (C2_times_SX_minus_CY < 0f) {
// imaginary answer
}
else {
// hokay
speed = Mathf.Sqrt(G_times_X_SQUARED / C2_times_SX_minus_CY);
}
```

you'll need to modify this to deal with the fact that your launch point is not at the origin (or reconfigure scene so that is), and also this formula gives you the launch speed, but you're asking for the launch force. i'm not sure how to compute the force from the speed, but hopefully that's relatively simple. also, if you allow the launch angle to be pointing in the opposite direction from the target, this formula will produce a wrong result, because it's assuming the "+" portion of "+/-" sqrt().

i put up a live demo here

fun stuff !

Thank you very much for your help!

Looks like this formula might work, but it gives me the distance, and not the velocity, and i don't know how to reverse it.

Anyways, i will see if i can figure it out.

This works super well! You can just set the velocity to speed, and it will work! **Thank you so much!**

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

Calculate Position of Rigidbody after applying Force 0 Answers

Resume movement of instanced object relative to the original after instantiation. 2 Answers

Why does writing to rigidbody.velocity after AddForce stop my rigidbody moving? 4 Answers

Manually Apply Cars Collision Response Force 0 Answers

RigidBody 2D x velocity function 1 Answer