In Unity, I'm trying to figure out how to rotate an object around a pivot point. I found a snippet of code over on the regular stack overflow, but it wasn't working even close to correctly. I have edited it a bit and now its starting to work a little better, but its still pretty off from what I need. It rotates around the object, but about half way through it jumps to another position. The one half actually rotates with the pivot point, albeit a little bit offset from where it should be, and if you keep rotating it the offset becomes greater and greater. Then the other half is just completely off, still not as bad as the original code though.
Sorry I'm having trouble explaining, here is a GIF to make it easier.
public Vector3 RotTesta(Vector3 point, Vector3 pivot, Quaternion pointRotation, Quaternion pivotRotation)
{
//To rotate an object around an arbitrary point, translate first (the arbitrary offset) then rotate as opposed to rotate-scale - translate
Vector3 offset = point - pivot;
Vector3 rotationOffset = pivotRotation.eulerAngles - pointRotation.eulerAngles;
offset = Quaternion.Euler(rotationOffset) * offset;
return offset + pivot; // calculate rotated point
}
This just handles its position, after you call this you would just do transform.rotation = pivotRotation.
If someone could please help me out with this I would be very grateful.