Funky Flying Quaternion Question
Hey all! I'm prototyping a gyroscope based flying game for Android. Here is my problem:
I have a target rotation (the orientation of the player's phone). The plane needs to rotate towards that direction at different speeds on each axis (so pitch and roll (X and Z) are snappy and yaw (Y) is ineffective). Here's what we're working from:
driver1.transform.rotation = Quaternion.Euler(Mathf.LerpAngle(plane.transform.eulerAngles.x, targetRot.eulerAngles.x, Time.deltaTime * pitch),
Mathf.LerpAngle(plane.transform.eulerAngles.y, targetRot.eulerAngles.y, Time.deltaTime * yaw),
Mathf.LerpAngle(plane.transform.eulerAngles.z, targetRot.eulerAngles.z, Time.deltaTime * roll));
I've got it working smoothly but the planes X and Y axis don't take the Z axis into account. When flying straight forward and right side up, everything works correctly - the yaw (Y axis) is sluggish because the yaw value is low.
But if I roll (Z axis) the plane 90 degrees, flying "sideways", the PITCH (the plane's up and down) becomes sluggish, because it's rotating around the WORLD's yaw (Y axis). I need the PLANE's left and right (yaw, Y axis) to always be slower no matter if it's aligned with the WORLD's Y axis or not.
Thinking that this is an issue of "local" and "relativeness" vs "worldness", I tried attaching the plane, still controlling the X and Y rotation, to a parent object, controlling the Z axis. But this resulted in some strange loopy behavior. Still, if this is the way to accomplish this, here is the code to be corrected:
driver1Z.transform.rotation = Quaternion.Euler(originalZRot.eulerAngles.x,
originalZRot.eulerAngles.y,
Mathf.LerpAngle(planeZ.transform.eulerAngles.z, targetRotation.eulerAngles.z, Time.deltaTime * roll));
driver1.transform.localRotation = Quaternion.Euler(Mathf.LerpAngle(plane.transform.eulerAngles.x, targetRotation.eulerAngles.x, Time.deltaTime * pitch),
Mathf.LerpAngle(plane.transform.eulerAngles.y, targetRotation.eulerAngles.y, Time.deltaTime * yaw),
originalRot.eulerAngles.z);
How can I make my adjusted Quaternion.Euler align with the plane's (it's OWN) Z axis instead of the world's, without having loopy (I'm assuming gimbal locked) rotation? Thanks a lot!
I see that you are using transform.eulerAngles, which provide the Euler angles for the WORLD-space roatation of the transform. I would suggest you try transform.localRotation.eulerAngles since you want to affect the speeds of the planes local (model-space) rotation.
Hey Glurth! I'm not quite sure how to implement what you suggest, and I have tried a few ways.
driver1.transform.rotation = Quaternion.Euler($$anonymous$$athf.LerpAngle(driver1.transform.*localRotation.eulerAngles*.x, targetRotation.eulerAngles.x, Time.deltaTime * pitch),
$$anonymous$$athf.LerpAngle(driver1.transform.*localRotation.eulerAngles*.y, targetRotation.eulerAngles.y, Time.deltaTime * yaw),
$$anonymous$$athf.LerpAngle(driver1.transform.*localRotation.eulerAngles*.z, targetRotation.eulerAngles.z, Time.deltaTime * roll));
The results are the same if I change the first "driver1.transform.rotation" to ".localRotation". I test this by setting my yaw speed to 0, rolling 90 degrees, and trying to "pitch" horizontally and I still haven't turned at all. Should I apply an additional rotation to the target direction to account for roll? Thanks dude.