- Home /
Combining two Quaternion Slerps on an object
Hey all,
So, I have a game that requires rotation around a pipe on four cardinal axes, as well as turning along curved tracks. What I have currently that work independently are two Quaternion.Slerps, seen here:
// Turns transform around pipe
transform.rotation = Quaternion.Slerp(prevTurn, nextTurn, t);
// Turns transform as it hits curved sections
transform.rotation = Quaternion.Slerp(prevTwist, nextTwist, tPos);
So, these both work by themselves, but any combination I've tried has failed. But I need the object to be able to curve along these pipes and spin around them simultaneously. Any ideas?
Thanks! :)
Answer by Kryptos · Jan 19, 2012 at 08:59 AM
Quaternions can be combined with *operator, according to the documentation (http://unity3d.com/support/documentation/ScriptReference/Quaternion-operator_multiply.html).
So you can do for instance:
// Turns transform around pipe
Quaternion qPipe = Quaternion.Slerp(prevTurn, nextTurn, t);
// Turns transform as it hits curved sections
Quaternion qTurn = Quaternion.Slerp(prevTwist, nextTwist, tPos);
transform.rotation = qTurn*qPipe;
Not that qTurn*qPipe and qPipe*qTurn will create different combination. You may need to try with one or the other.
I've tried every form of this that I could, and it never works out satisfactorily. It never ends up with my character on cardinal directions. I always end up with awful, mixed up rotations at the end of the pipe sections.
Well then, the problem is no the quaternions but maybe your t and tPos parameters.