- Home /
How to rotate a vector
I'm writing a simple control function for my airplane-like object with airplane-like controls. I'd like to use my velocity vector to position my lift vector, such that I rotate the velocity vector 90 degrees in pitch and then rotate that vector by the angle of bank, which will result in a vector in the 'lift' direction. Unity has plenty of good support to rotate the transform, but I'm not trying to rotate my player, I just want Unity to compute the quaternion rotation for me. Here's what I've tried to do, but Unity is not treating the multiplication of my vector by quaternion as a true movement of the vector.
//determine lift vector direction
**Quaternion rotateLift1 = Quaternion.Euler (90.0f, 0.0f, subBank);**
// Quaternion rotateLift2 = Quaternion.Euler (0.0f, 0.0f, subBank); //rotate velocity vector to make lift vector
**Vector3 liftVector = rotateLift1 * V;**
// Vector3 liftVector = Quaternion.FromToRotation(V, rotateLift1); // normalize lift vector // forward = forward.normalized;
**liftVector = liftVector.normalized;**
// multiply lift vector by lift magnitude to determine actual lift vector
**Vector3 L = lift * liftVector;**
I've left some of my failed code attempts in, for further information. I've put the current code in bold. I really don't know what to do at this point, short of coding my own quaternion transform, which is a bit more than I care to take on, particularly as it seems as though Unity should have a simple solution. Any thoughts? Thanks!
$$anonymous$$aybe I'm stating the obvious here, but can you just use transform.up as your lift vector?
Answer by gfoot · Feb 02, 2014 at 09:28 PM
The problem with the third line of your code is that Unity's Euler function applies the rotations in the wrong order for you. You can split them up into two calls and multiply them yourself whichever way you like, e.g.
Quaternion.Euler(0, 0, subBank) * Quaternion.Euler(90, 0, 0)
or vice-versa.
Because Unity multiplies vectors on the right of quaternions, you can read this right-to-left as a sequence of rotations about the world axes - i.e. 90 degrees about the world X axis, then subBank degrees about the world Z axis - or from left-to-right as a sequence of rotations about local axes - i.e. subBank degrees about the Z axis, then 90 degrees about the new local X axis.
Sorry to take so long replying, but I just returned from a conference. Anyway, gfoot, I had tried reordering and breaking down the rotations in various ways, but the way unity 'multiplies' vectors, it is not a true rotation, and my recollection is that the scalar quantities actually multiply each other, and it just wasn't giving me the correct answer. $$anonymous$$y solution was to algebraically deter$$anonymous$$e the zero-bank lift vector, based upon it being orthogonal. Then I computed the quaternion myself and multiplied it out, and voila. @AdamScura, you might have saved me a few hours of math had I not already deter$$anonymous$$ed a solution. I haven't tested transform.up. I thought previously that all transform functions directly manipulated the transform, so I didn't read through all of them, but from what I could tell from the manual, it should work. I might change it eventually to clean it up and maybe improve processing, although Unity is likely computing a solution the same way. Thanks!
Your answer
Follow this Question
Related Questions
Rotate vector around vector? 2 Answers
Rotating a character's velocity? 3 Answers
Quaternion.Slerp only one axis? 1 Answer
Snap and align two cubes 1 Answer