- Home /
Rigidbody AddTorque() stops working occasionally
In a simple 2.5D shooter, I have a spaceship that rotates around the y axis. All rotation is controlled through the Rigidbody (using AddTorque) and it is constrained in the other 2 axis. The ship also thrusts forward (using AddForce). Simple enough.
Occasionally and for no reason I can discern, the AddTorque stops having any effect. The AddForce continues to work. The ship remains unable to turn until it is involved in a collision. Then the AddTorque suddenly works again. A collision with a trigger has no effect.
I'm at a loss as to how to investigate this. Any suggestions?
Well, how do you use AddTorque? Which values do you pass in? If your ship is aligned in worldspace and should only rotate around the worldspace y axis (in the x-z-plane) you would just use
rb.AddTorque(0, yourTorque, 0);
When you said "stops working", does that mean it doesn't apply any force anymore but continues to rotate? Do you actually apply angular drag? Are you sure you don't apply any other forces or setting the angular velocity manually somewhere?
If it gets stuck, does it get stuck in a certain orientation or is it arbitrary? If it's always the same you might be using the x-y plane and rotate around z and you turn the ship into a gimbal lock position. Unity as well as PhysX itself uses quaternions for all rotations. However maybe you or Unity does some euler angles conversion at some point which would re-introduce the gimbal lock problem.
Since we don't know anything about your setup (orientation in worldspace, how torque is applied, if there are other components messing with the angular velocity / rotation) we can't really answer this question without more information from your end.
The ship is aligned with world space and the torque is applied using:
rb.AddTorque(Vector3.up*diff*Time.fixedDeltaTime*20);
where diff is the angle between the z axis and the desired z axis. I've debugged diff and it is still behaving as expected.
The rigidbody has a 3.5 angular drag.
The only other rotation that is applied is to a child transform that has no rigidbody. This is done with transform.GetChild(0).Rotate(). I assume this wouldn't make any difference.
There doesn't seem to be any reason as to when it stops working. The angles vary and it doesn't seem to follow any event. I would guess that it is only that the force is no longer applied but that it continues it's rotation until the drag stops it but it's hard to tell.
It needs a collision that changes its orientation for it to start working again.
Answer by Idual · Sep 08, 2019 at 02:32 PM
I have managed to solve this one. The problem was that at some point I had rotated the GameObject by 45 degrees in the editor. I assume this caused some kind of gimble lock. Thanks to Bunny83 for the reply. It helped me focus on the areas to look in which is what I was after.
Your answer
Follow this Question
Related Questions
Ball Rotating Help 0 Answers
Camera to rotate around object as well as follow it at the same time. 3 Answers
Move a rigid body in an area of a platform 0 Answers
Rigid body rotation question 1 Answer
Rigidbody will not rotate correctly 2 Answers