- Home /
Rotate a vector around a certain point.
I have already tried transferring my point into a transform, using t.RotateAround, and converting it back into a Vector, but in return, it is giving me a weird error. Instead of trying to fix it, I want to use a better method of doing this. So what would I have to do to rotate my Vector3 around a specific point?
Answer by aldonaletto · Sep 07, 2013 at 01:45 PM
If you want to rotate a vector, multiply it by a Quaternion. Supposing that you want to rotate a vector 60 degrees about the world X axis, for instance:
var myVector: Vector3 = Quaternion.Euler(60, 0, 0) * Vector3.forward;
myVector becomes the vector (0,0,1) rotated 60 degrees about X.
But be aware that a vector indicates a direction in space - it's not tied to any specific position, thus rotating a vector around a point doesn't make any sense. If you actually want to rotate a point around a pivot instead, get its direction relative to the pivot and rotate it, then add the rotated direction to the pivot:
function RotatePointAroundPivot(point: Vector3, pivot: Vector3, angles: Vector3): Vector3 {
var dir: Vector3 = point - pivot; // get point direction relative to pivot
dir = Quaternion.Euler(angles) * dir; // rotate it
point = dir + pivot; // calculate rotated point
return point; // return it
}
Can you help me convert the above one to c#? There is a bug in the first line.
public Vector3 RotatePointAroundPivot(Vector3 point, Vector3 pivot, Vector3 angles) {
Vector3 dir = point - pivot; // get point direction relative to pivot
dir = Quaternion.Euler(angles) * dir; // rotate it
point = dir + pivot; // calculate rotated point
return point; // return it
}
Hey so I am thinking of using this to add to my "camera follow" script, but I am having a hard time understanding how to apply it. In the script posted what is "Vector3 angles" equal to?
PS : Sorry for opening an old post but I have been looking all over on how to achieve this with no luck. Basically, I am trying to have the camera rotate around a plane/spacecraft from behind with the craft as the pivot.
hi there, the main error is that i worked for 3-4 hours to figure out the rotation, and after many hours of angst, i came back to this page, i read the first 3 lines you wrote attentively and slowly, and then i had the required code in 2-3 $$anonymous$$utes. :D
A question, can this be used to rotate a vector or a transform so that it maintains it's current direction but with a new rotation?
Answer by Guenter123987 · Jan 31, 2017 at 04:11 PM
If you need to use the RotateAroundPivotPoint function very often this could improve your performance:
public Vector3 RotatePointAroundPivot(Vector3 point, Vector3 pivot, Vector3 angles) {
return Quaternion.Euler(angles) * (point - pivot) + pivot;
}
It might work better because there are not so many variables in the code which have to be managed.
Also if you want to be able to pass a quaternion or some angles:
public static Vector3 RotatePointAroundPivot(Vector3 point, Vector3 pivot, Vector3 angles) {
return RotatePointAroundPivot(point, pivot, Quaternion.Euler(angles));
}
public static Vector3 RotatePointAroundPivot(Vector3 point, Vector3 pivot, Quaternion rotation) {
return rotation * (point - pivot) + pivot;
}
I'm rotating around 0,0,0, so I'm using this method... Vector3 rotateAround(Vector3 point, Quaternion rotation){ return point * rotation; } It doesn't seem to work. Help?
Answer by FlightOfOne · Dec 28, 2017 at 07:01 PM
Thank you all for the above answers. Helped me a lot. but took some time to wrap my mind around this to fit this solution to what I needed to do. I wanted to have a transform move to a new point and rotate it to match this new point's rotation. But the original transform has a pivot that it needs to rotate around that is not at the center. Posting this here so anyone else that might want to do the same can benefit without having to spend too much time figuring it out.
public class RotTester : MonoBehaviour
{
[SerializeField] Transform _pivotPosition;//parented to this transform
[SerializeField] Transform _finalPosition;//I want to move this transform to here
// Use this for initialization
void Start()
{
//I want to move this object to the target Position
//First move to target
transform.position = _finalPosition.position;
//set the final position after rotating around the pivot and offset the pivot local position
transform.position = RotatePointAroundPivot(_finalPosition.position, _pivotPosition.position, _finalPosition.rotation)- _pivotPosition.localPosition;
//finally set the rotation to target rotation
transform.rotation = _finalPosition.rotation;
}
public Vector3 RotatePointAroundPivot(Vector3 _finalPosition, Vector3 _pivotPosition, Quaternion _finalRotation)
{
return _pivotPosition +(_finalRotation * (_finalPosition - _pivotPosition)); // returns new position of the point;
}
}
Glad you got it working! I think the link after "First move to target" is unneeded. RotatePointAroundPivot is already using the final position and applying it to transform.position. But other then that it looks legit.
I thought the same, but it goes all over the place for me if I don't set the position first. $$anonymous$$aybe I am not seeing something in rest of my code.
oh odd. I could only see that happening if _pivotPosition is nested in transform or something. But if it ain't broke don't fix it I suppose!
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Object Rotating around center from Mouse Movement 1 Answer
Rotating a Gameobject In Three Axis Unity 3 Answers
Camera rotation 0 Answers
Problems Scripting a Camera 1 Answer