- Home /
How to create quaternion defined by angle and up vector
So I want to create rotation defined by up vector and angle. I tried Quaternion.AngleAxis(angle, transform.up) but it doesn't work. transform.RotateAround works but I don't want to rotate by some angle, I want to create rotation with exact angle.
Answer by Bunny83 · Apr 25, 2020 at 12:33 AM
So if you want to rotate an object around the local up vector relative to the worldspace forward axis you can do:
Vector3 fwd = Quaternion.Euler(0, angle, 0) * Vector3.forward;
Vector4 right = Vector3.Cross(upVector, fwd);
fwd = Vector3.Cross(right, upVector);
Quaternion q = Quaternion.LookRotation(fwd, upVector);
This should calculate a rotation around the provided up vector relative to the world forward axis.
Answer by CiberX15 · Apr 23, 2020 at 12:58 AM
Sounds like what you are looking for is Quaternion.LookRotation(forwardVector, upVector); https://docs.unity3d.com/ScriptReference/Quaternion.LookRotation.html
but I don't know how to get this forward vector, I can't rotate vector3 because it will ignore existing rotation, and I can't use transform.forward because it will add to rotation and not set rotation with a specified angle and the object will spin like crazy.
Ah, you can get a rotated forward vector without actually rotating anything by multiplying by a Quaternion.
Vector3 rotatedForward = Quaternion.Euler(0, angle,0) * transform.forward;
That would give you the vector as if you had rotated your transform angle degrees on the Y axis. If you then wanted to set something to that rotation you could combine in with Quaternion.LookRotation() as such:
Vector3 rotatedForward = Quaternion.Euler(0, angle,0) * transform.forward;
transform.rotation = Quaternion.LookRotation(rotatedForward, Vector3.up);
Sorry for being annoying but this is not it, it will infinitely rotate on the object because we always rotate forward vector what I want is to rotate the object to specified angle.
Your answer
Follow this Question
Related Questions
Smoothly rotate object based on GetAxis 1 Answer
Build rotation tool for level editor 0 Answers
Rotating Cube with oculus touch controllers 0 Answers