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

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;

}
``````

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

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){
float xComponent = Mathf.Cos(angle) * force;
float zComponent = Mathf.Sin(angle) * force;
Vector3 forceApplied = new Vector3(xComponent, 0 , zComponent);

}
``````

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!

@metalted Hi, first of all, thanks a lot. Your answer helped me a lot and it works great. I was trying to shoot multiple projectiles and working on your code did it well! However, I only manage to shoot at certain angles in only one direction but I want to shoot at with same angles based on my rotation. How can I keep the angle same based on my rotation ? Can you think of anything to work around this ? This is my first post/comment so I don’t know if I should continue here or post another question to forum.