- Home /

# Can you add a force to a rigidbody at an angle?

Hi! I am making a game where you control a sphere by adding forces to the rigidbody of said sphere. To accomplish that I would like to be able to add a force to the rigidbody around the y axis of rotation(not relative to the sphere). I can't make this relative to the sphere since it will bump into other objects and the physics engine will rotate the sphere. It's a little confusing so I have an image that might help visualize my idea. Image Link

**Answer** by GlassesGuy
·
Jul 06 at 01:48 AM

Thanks to @metalted I started looking up equations to turn an angle into x and y coordinates. I found this and adapted it to work with my game. Here's the code

```
public void AddForceAtAngle(float force, float angle)
{
float xcomponent = Mathf.Cos(angle * Mathf.PI / 180) * force;
float ycomponent = Mathf.Sin(angle * Mathf.PI / 180) * force;
playerRB.AddForce(ycomponent, 0, xcomponent);
}
```

This works great and you can modify the Vector3 for adding a force on your rigidbody and change what plane you want it on.

**Answer** by metalted
·
Jul 05 at 09:11 PM

You want to convert an angle to force applied to the x and z direction? You could use cosine and sine for this. This will convert the angle into 2 components that can be applied to the object. When using cos and sin, the 0 degree angle will be at the right side of the object. I hope my explanation is clear, but the code will make it easy to understand. Lets say we want to move the object to the top-right with a force of 500 units. Because the 0 degree angle is at the right side of the object, the top right "corner" of the circle will be at 45 degrees:

```
public void AddForceAtAngle(float force, float angle){
angle *= Mathf.Deg2Rad;
float xComponent = Mathf.Cos(angle) * force;
float zComponent = Mathf.Sin(angle) * force;
Vector3 forceApplied = new Vector3(xComponent, 0 , zComponent);
rb.AddForce(forceApplied);
}
```

So lets do the math. If we have an angle of 45 degrees Cos(45) will be 0.707, Sin(45) will be 0.707 as well. Multiplying these values with our force of 500 will give us the following components: x = 353, z = 353. If we calculate the resulting vector (using Pythagoras) we get a value of 500. Because the cos and sin work with any angle, you can also enter a value above 360 degrees and get a good result. Hope this helps!

After some testing it doesn't seem to give the same results you said they would. I end up with X= 262.660994409 and Y = 425.451762267. Is there something I'm missing? I ran the numbers in my calculator and got the same numbers I did in Unity.

@GlassesGuy Ahh I see the problem, its a radians degrees issue. Some calculators use degrees, some use radians. I created the answer using my calculator, which uses degrees. Your calculator, and Unity so it seems (oops) make use of radians. To fix this you can simple multiply the angle entered with Mathf.Deg2Rad or you'll have to convert the angle beforehand, but that's not really user friendly. I'll update the code so it will give the right answer when entering an angle in degrees.

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

Prevent Rigidbody From Rotating 3 Answers

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

An unexpected physics behavior 2 Answers

Rigidbody "hopping" when crossing objects. 2 Answers

Rotate Rigidbody on Two Separate Axes Independently to a specific angles 0 Answers