Calculate rotation speed near 180°/-180°


I’m using the function Rigidbody.MoveRotation in my script to rotate a cube. In the inspector, the rotation appears to go from 180° to -180°.

I want to write a simple script to find out how fast the cube is rotating. At first I wrote a simple:

Mathf.Abs(current_angle - previous_angle)

where the previous angles is the rotation angle from the previous fixedupdate. This does not always work though, since when the previous_angle is, say 170, and a rotation of 20 degrees happens, the current_angle becomes -170.

Is there a simple way to fix this? Right now I’m using an IF to check whether or not the two angles are near 180°, and I’m using a custom calculation for that case, but if the rotation speed is high, this could not work either.

Well, if you’re already using MoveRotation, then the values you put in will be the rotation coming out, so you should already know it, right?

If the Rigidbody is rotating on it’s own, it exposes an angularVelocity field that will tell you exactly how fast it’s rotating on each axis, so you can look it up there without calculating it yourself.