Question by
papathor · Jul 19, 2016 at 08:18 PM ·
rotationquaternioneulerangles
What's wrong with this Quaternion rotation?
I'm writing a script to level a drone's tilt so it is flat. I have a quaternion for the start position I want to rotate to, and a vector for the current position. Since I only want to level the drone, not change its orientation, the rotationQuaternion (the target) uses the y coordinate that the drone currently has.
However at present the drone just snaps to an awkward 45 degree angle. How do I get smooth rotation to a level point while maintaining orientation?
The below code is in FixedUpdate()
Quaternion startRotation = new Quaternion(-0.5f, 0.5f, 0.5f, 0.5f);
Vector3 rotationQuaternion = new Vector3(startRotation.eulerAngles.x, transform.eulerAngles.y, startRotation.eulerAngles.z);
Vector3 currentPosition = new Vector3(transform.eulerAngles.x, transform.eulerAngles.y, transform.eulerAngles.z);
transform.rotation = Quaternion.FromToRotation(currentPosition, rotationQuaternion);
Thanks for the help!
Comment
Answer by Soos621 · Jul 19, 2016 at 09:38 PM
You can tilt the rotation over time use Quaternion.slerp or vector3.slerp to make the rotation smoothly rotate over time.