- Home /
Vector3 Rotate Around
I have been using Transform.RotateAround to rotate a transform around a pivot but now I need to rotate a Vector3
around a point and I am stumped how to do it.
I am honistly considering backing up a transform's position and rotate and using it to hold my point but that would be poor programing. So my question is, is there a function to rotate a Vector3
around a pivot?
Thanks
Answer by Peter G · Feb 11, 2011 at 03:33 AM
I don't know of any built-in function, but you could create your own. Here's a quick example of something you could do.
static function RotateAroundPoint(point : Vector3, pivot : Vector3, angle : Quaternion) : Vector3 { var finalPos : Vector3 = point - pivot; //Center the point around the origin finalPos = angle * finalPos; //Rotate the point.
finalPos += pivot;
//Move the point back to its original offset.
return finalPos;
}
You can create the angle using Quaternion.Euler();
var newPos = SomeClass.RotateAroundPoint(Vector3.forward, Vector3.zero, Quaternion.Euler(0, 90, 0) );
Debug.Log(newPos);
//Prints (1, 0, 0) or is it (-1, 0, 0 )? Something like this. :)
I'm not sure why I got voted down. If its a math error, please tell me so I can fix it, but I think it is all sound even if not the most efficient method.
Thank you peter this was exactly the thing i was looking for it is working perfectly.
slightly more optimized:
function RotateAroundPoint(point : Vector3, pivot : Vector3, angle : Quaternion) : Vector3 {
return angle * ( point - pivot) + pivot;
}
Works spiffingly! Thanks, $$anonymous$$isters Peter G and $$anonymous$$iketucker.
Thanks Peter! I've been trying to figure this out for hours and this solution works perfectly!
Answer by WidmerNoel · Apr 30, 2015 at 06:44 PM
I think the solutions provided above can be improved a little more. These are extension methods, which behave as if they actually were memebers of Vector3 and Transform. (As if RotateAround would still exist)
public static class RotateAroundPivotExtensions {
//Returns the rotated Vector3 using a Quaterion
public static Vector3 RotateAroundPivot(this Vector3 Point, Vector3 Pivot, Quaternion Angle) {
return Angle * (Point - Pivot) + Pivot;
}
//Returns the rotated Vector3 using Euler
public static Vector3 RotateAroundPivot(this Vector3 Point, Vector3 Pivot, Vector3 Euler) {
return RotateAroundPivot(Point, Pivot, Quaternion.Euler(Euler));
}
//Rotates the Transform's position using a Quaterion
public static void RotateAroundPivot(this Transform Me, Vector3 Pivot, Quaternion Angle) {
Me.position = Me.position.RotateAroundPivot(Pivot, Angle);
}
//Rotates the Transform's position using Euler
public static void RotateAroundPivot(this Transform Me, Vector3 Pivot, Vector3 Euler) {
Me.position = Me.position.RotateAroundPivot(Pivot, Quaternion.Euler(Euler));
}
}
/*USAGE:
*
* myGameObject.transform.RotateAroundPivot(CenterAsVector3, AngleAsVector3); //Modifies the Transform (immediate rotation)
* myGameObject.transform.position.RotateAroundPivot(CenterAsVector3, AngleAsVector3); //Returns the rotated position (preview rotation)
*
*/