- Home /
How to smoothly rotate an object through just one axis
Hi,
**Edit:* I've changed my question as I need to rotate an object smoothly through just one axis, and realise that I am probably going about this in the wrong way by using Quaternion.Slerp, etc. Any advice on how to achieve this is most welcome. I have still left my original question below to give you a picture of where I'm going wrong.
I'm rotating an object with Quaternion.Slerp, and then checking if it is finished by using Quaternion.Angle. and using an if statement to check if it's reached its destination. Once it reaches its destination it will stop trying to Slerp to it. My problem is that I only need to check one or two of the axes (it should just be the one axis I need to check if my logic serves correctly!)
My code is simply this:
if(Quaternion.Angle(toUpsideDown.rotation,transform.rotation) == 0){
FinishedRotation();
}
How can I isolate/exclude individual axes from Quaternion.Angle? I only want to know when the x and y have reached their destination, I don't want it to wait for z as well as this causes behaviour that I don't want (I want it to keep facing the same direction when it is rotated, no matter where it is looking - not be forced to turn and face north, for example)
My destination in Quaternion.Angle is a Transform variable (called toUpsideDown as the object is rotated to be upside down), and to use it I drag an empty GameObject into the inspector which is at the right orientation.
If I cannot achieve this with Quaternion.Angle, what other methods can I use?
Thanks!
I don't think it works that way. A slerp just goes straight from one facing to another. Every "axis" gets there at the same time.
Usually with problems like this, it's a matter of figuring out what you really want to do.
You're probably right, I hope I can figure it out with some help. What I really need to do is smoothly transition 90 degrees through just one axis. I knew how to use Lerp to move an object, but I'd read that to smoothly rotate an object, you need to use Quaternion.Slerp? So, is there another way to "lerp" between rotations without having to influence all three axes? I am still learning to program so there's likely something to do this that I haven't heard of.
Answer by quantumface · Oct 17, 2014 at 01:03 AM
You wrote Quaternion.Slerp at the start, and Quaternion.Angle the rest of the post. Confusing. But from the documentation of both, you can see that they use start and end positions as Quaternions. And you can replace those quaternions pretty much with Vector3(x, y, z), so you can just use your own values on any of the axes you need. I hope makes sense? :)
Ah sorry, I meant that I am slerping the object with Quaternion.Slerp, then using Quaternion.Angle to check if the slerp is finished. Could you give an example of how to replace the Quaternions with my own values?