Setting a quaternion's rotation value
Hi,
I'm writing a script to auto-level a drone in a game, and I've come across a tricky problem that I'm not sure how to resolve. When the AutoLevel function begins, I create a Quaternion with the x,y,z,w values for a perfectly level drone facing forward. Originally I was creating this in Start() without explicit x, y, z, w values but I had an issue with startRotation changing values so I for now I just create a new one every FixedUpdate cycle, I'll refactor this when everything else is working.
Quaternion startRotation = new Quaternion(-0.5f, 0.5f, 0.5f, 0.5f);
// need something like startRotation.eulerAngles.z = transform.rotation.z
// so the target rotation is facing in the same direction
transform.rotation = Quaternion.RotateTowards(transform.rotation, startRotation, 1.0f);
I need the drone to autolevel but while maintaining its z-axis rotation, not the startRotation z-axis rotation (which is just straight ahead). So if the drone is facing right, it will auto level while still facing right. (And when I refer to the z-axis I mean the euler angle z, not the Quaternion z which I of course wouldn't modify.)
How do I update the quaternion startRotation so its direction matches the current transform.rotation.z?
Thanks for the help!