- Home /
Mass not effecting torque
Hi all,
I'm making a 2D space game where the heavier the ship gets, the harder it is to move/steer. While the linear movement is effected as the rigidbody's mass increases, the angular acceleration is not. Here's my code:
rb.AddTorque((Input.GetAxis("Turn") * tmpEnginePower * rateOfTurn), ForceMode2D.Force);
With or without the ForceMode2D argument the same thing happens. The same rate of torque rotates the rigidbody at the same rate no matter what its mass is. Different torque values do change the angular acceleration but the mass doesn't. Changing the torque value based on the vehicle's mass seems like a really backwards way to achieve the behaviour I want. Angular drag works as expected but that's not what I'm after as the game is in space, hence no drag. What am I missing?
Answer by _Prism_ · Nov 14, 2019 at 03:02 AM
Ooookay, so as I suspected, I was missing something. In my defense it's easy to miss. I know in real world physics, the phenomenon I'm describing is inertia, but there's no inertia setting in the inspector component of a Rigidbody2D. Except there is - it's buried in the Info box of the component (which closes every time you enter play mode, so that doesn't help) and unlike the angular drag variable it can not be modified in the inspector - it's greyed out. The inertia variable is however accessible via script, so now I set the inertia float whenever the ship's mass changes. Heavier the ship gets, the harder it is to turn. Exactly what I want.
Nice to see people posting their solutions ins$$anonymous$$d of just closing the question with 'Solved on my own'.
I figured it might be useful for someone else if they miss the same thing I did =)
Normally the inertia tensor is calculated from the shapes and mass of attached colliders, and possibly automatically recalculated when they move. Setting the intertia tensor by script will disable this automatic feature, probably that's why it's greyed out. To re-enable automatic inertia tensor calculation, call Rigidbody.ResetInertiaTensor();
Thanks man, that's good to know. As I'm updating the inertia manually at the same time I update the rigidbody's mass, the effect of inertia should be correct shouldn't it?
Note that the story is slightly different for Physics2D which uses a completely different physics engine. You do not have a tensor but just a single inertia value since we only have one rotation plane in 2d. I have never used Unity's 2d physics (which uses the Box2D engine). However Collider2D seems to have a density property and when actually using it you don't want to set the mass manually. I'm not sure if the inertia is actually calculated automatically when "useAuto$$anonymous$$ass" is true. That's something someone has to test since the documentation isn't explicit here.
Interesting. I'm going to do some testing to get the 'feel' of what mass values to use with what engine power/torque values, so I'll have a look at that too =)
Your answer
Follow this Question
Related Questions
2D Torque rotation - stop at certain angle 0 Answers
rigidbody2D.AddTorque() works differently on iOS, in editor and in web player 1 Answer
Angular velocity not affecting Rigidbody2D 1 Answer
How to make a rigid body move along a specific path using my mouse? 1 Answer
Problem with Rigidbody2D.angularVelocity 0 Answers