1
\$\begingroup\$

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.

\$\endgroup\$
1
\$\begingroup\$

There's built in functionality for this: RotateAround.

Rotates the transform about axis passing through point in world coordinates by angle degrees.

This modifies both the position and the rotation of the transform.

Basically, pick a point and an axis. Then rotate around that axis by the degrees specified. Example:

using UnityEngine;
using System.Collections;

public class ExampleClass : MonoBehaviour {
    void Update() {
        transform.RotateAround(Vector3.zero, Vector3.up, 20 * Time.deltaTime);
    }
}
\$\endgroup\$
  • \$\begingroup\$ Yeah I actually do know about it, thanks though. I was hoping to learn a little math, and I actually think that this wont work for what I need to do, cause I need to set the offset from the difference in rotations, and this is only good for rotating by an angle. \$\endgroup\$ – LittleRain Jul 1 '17 at 2:07
  • \$\begingroup\$ Calculate the angle difference and apply that \$\endgroup\$ – MichaelHouse Jul 1 '17 at 2:21
  • \$\begingroup\$ I've never had so much trouble with anything before. How can I calculate the axis to rotate on? \$\endgroup\$ – LittleRain Jul 2 '17 at 3:30
  • \$\begingroup\$ Well, if this is the most troubling thing in your life, you're a very lucky person. The axis to rotate on will likely be the right or left vector to your object transform. In your example gif, you can see it's rotating around the X axis. \$\endgroup\$ – MichaelHouse Jul 2 '17 at 5:45
  • \$\begingroup\$ But what if I want to be able to rotate on the other axis', or do a free rotate? This problem may seem simple, and it could be, but it will make things limited. I know I could do Vector.right/up/forward, but I will have to make a setting to change which axis I would like to rotate on, instead of being able to use the rotation handle. Which I'm trying to avoid that. \$\endgroup\$ – LittleRain Jul 2 '17 at 22:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.