# How To Rotate A Vector around A Pivot Point

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.

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);
}
}

• 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. – LittleRain Jul 1 '17 at 2:07
• Calculate the angle difference and apply that – MichaelHouse Jul 1 '17 at 2:21
• I've never had so much trouble with anything before. How can I calculate the axis to rotate on? – LittleRain Jul 2 '17 at 3:30
• 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. – MichaelHouse Jul 2 '17 at 5:45
• 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. – LittleRain Jul 2 '17 at 22:32