How to prevent object from drifting when using transform RotateAround

Hello,
I’m using RotateAround to make an object spin around it’s center. Problem is that after a while the object drifts off it’s position, upwards in my case. So after a few minutes the GameObject has wandered up up and away. I suspect this is because the object is rotated at a “three quarters” angle.

To fix this I store it’s position before the call to RotateAround, then restore the position afterwards.

Unfortunately this nulls out the effect of rotating around it’s center, and the object rotates around it’s 0,0,0 position, which is no good. This is my rotation code:

Vector3 pos = obj.renderer.bounds.center;
obj.transform.RotateAround(pos, obj.transform.up, rotateSpeed);

which has the effect of making the object slowly drift upwards. Then storing/restoring:

Vector3 pos = obj.renderer.bounds.center;
Vector3 position = obj.transform.position;
obj.transform.RotateAround(pos, obj.transform.up, rotateSpeed);
obj.transform.position = position;

which has the effect of making the object rotate around it’s 0,0,0 position rather than the center.

any suggestions on how to address either of these issues?
many thanks.

I just had this issue too. There seems to be a large amount of floating point error accumulation when using RotateAround in this manner. A more stable solution for local rotation is:

obj.transform.rotate = Quaterion.AxisAngle(obj.transform.up, rotateSpeed) * obj.transform.rotate;