- 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, 2019 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, 2019 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 $$anonymous$$athf.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.
Answer by AonaS · Aug 09, 2021 at 08:22 AM
@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.