- Home /
Rotating around a pivot point using a Quaternion?
I'm currently programming VR camera controls. I want to make it so when you click with the controller, the view rotates based on how you rotate the controller.
I was able to get this far,
Quaternion rot = start_rotation * Quaternion.Inverse(current_rotation);
transform.rotation = rot * transform.rotation;
This rotates, however I need to use the camera/head transform as a pivot point. I know that people use transform.RotateAround, however I havn't been able to figure out how to rotate using the offset I get by comparing the two rotations.
Answer by Namey5 · Jul 16, 2020 at 03:36 AM
Here is a basic implementation of RotateAround;
static void RotateAround (Transform transform, Vector3 pivotPoint, Vector3 axis, float angle)
{
Quaternion rot = Quaternion.AngleAxis (angle, axis);
transform.position = rot * (transform.position - pivotPoint) + pivotPoint;
transform.rotation = rot * transform.rotation;
}
The basic idea is to move the rotation's origin to the pivotPoint by taking the vector between it and the transform's position, then move back after - the rotation is then applied like normal. If you want to do the same with your own rotation, then you can just replace the call to AngleAxis;
static void RotateAround (Transform transform, Vector3 pivotPoint, Quaternion rot)
{
transform.position = rot * (transform.position - pivotPoint) + pivotPoint;
transform.rotation = rot * transform.rotation;
}
I must have changed the implementation slightly when posting - it should be using the right position now.
can you please convert this into coroutine and make it lerp to new position and rotation ?
Your answer
Follow this Question
Related Questions
I have a question involving FBX objects and their pivot points and rotations in world space. 0 Answers
Issue with rotation of an animation 1 Answer
Calculate Vector3 for rotation or just change pivot; 0 Answers
Camera following Pivot not working 1 Answer
How do I rotate in the center after changing the position of the pivot? 1 Answer