- Home /
combine / convert rotations (Quaternion and Vector3)
Hi, i've been trying to convert / combine two rotation transforms into one to stop them overriding each other.
//----- tilts character to surfaces -----//
transform.up = Vector3.Lerp(transform.up, surfaceNormal, Time.deltaTime * xRotSpeed);
//----- tilts character to surfaces -----//
//----- tilts left and right ------//
transform.eulerAngles = Vector3(0, curAngle, 0);
//----- tilts left and right ------//
I'm trying to either multiply / scale two together, to allow an X rotation to occur based on the surfaceNormal, or somehow convert the current Vector3.Lerp to allow me to extract a value to put into the X axis of the EulerAngles, as my character only has movement on the x and y (x for turning towards surfaces and y to rotate yaw based on forward velocity)
I hope you can help this hasgot me raging alot lately, tried so many Quaternion types and Vector scales and lerps its driving me nuts!
Thanks! Renn
Answer by Owen-Reynolds · Jun 27, 2012 at 03:30 PM
This is an untested variant of a working "angled with ground, but my spin" merged with your code. The way facingQ
is created/used is probably the most interesting part:
// your lerp to smooth into normal:
Vector3 newUpV=Vector3.Lerp(transform.up,surfaceNormal,Time.deltaTime*xRotSpeed);
// convert new up into a rotation:
Quaternion newUpQ=Quaternion.FromToRotation(Vector3.up, newUpV);
// your spinning:
Quaternion facingQ=Quaternion.Euler(0,curAngle,0);
// combine head tilt and spin:
transform.rotation = newUpQ * facingQ;
The problem with setting transform.up
then transform.eulerAngles
is that there's no good way to say "keep the work from the last instruction." Building quaternions for each local spin lets you combine them with *
. In this case, newUpQ
is the tilt that takes from facing North, head up, to still facing north-ish, but head tilted to the target. Then *facingQ
adds a local y-spin.
Thanks Owen, this worked perfectly, what you said about "building quaternion for each spin" only brings another question which is what is the difference between a quaternion and a vector (is it one is x,y,z, and one is x,y,z,w?)
Its something i will have to learn properly but a quick explanation would be grand, seeing as you understand that process clearly (as it worked a charm)
Thanks again, i now know you can multiply quaternions together to allow rotations on multiple axis ^_^
The x/y/z of Quats is just book-keeping stuff, with no useful meaning. A Quaternion is a complete "rotation/facing," ready to be assigned to your rotation. You could think of a Quat as a magic wand waved over the vector you want to face, plus the extra "spin" around it (the wand part makes it count as a rotation.)
A vector could be a way you want to be facing. If you use Quaternion Q=Quaternion.LookRotation(V)
, then Q
is that facing (with your head spun "up".)
The x/y/z/w quaternion notation has no simple/obvious "real world" representation, most notably the "x/y/z" is not directly related to actual X/Y/Z axes/rotations, it's just a way of accessing the Quaternion's members. You can convert a Quaternion into an axis+angle representation (where x/y/z would define the rotation axis, and w the angle around that axis), but that's not how quaternions are stored in Unity, and there is no trivial conversion (it involves asin/acos and sqrt).
If you must know, quaternions are represented in a 4-dimensional complex space :-P (and even that isn't the mathematically sound interpretation).