Basically what I want is to have the camera always look at the camera which can be achieved with the following LookAt rotation code:
transform.LookAt(transform.position + Camera.main.rtansform.rotation * Vector3.forward,
Camera.main.rtansform.rotation * Vector3.up);
HOWEVER, I want this to ALSO stay in place even when the phone rolls about itself 90 degrees. The problem is the pitch flips around 90 degrees, which doesn’t allow the look at to be proper when the phone is looking directly at the UI.
My code to attempt this is:
// 180 about z (roll) in order to counter the phone's rotation
Quaternion antiRoll = Quaternion.Euler(camRotation.eulerAngles.x, camRotation.eulerAngles.y, 180f);
Quaternion savedRot;
// The above was almost good, but we have to flip rot upside down AND backwards in world space
// in order to get it in proper world space
savedRot = antiRoll * Quaternion.Euler(180f, 180f, 0);
transform.LookAt(transform.position + targetTransform.rotation * Vector3.forward,
savedRot * Vector3.up);
I guess my questions are:
(1) - Why does this happen? Gimbal lock? Can anyone explain in depth.
(2) - I feel as if this is close, do I need to combine rotations or something?
Video showing problem when the pitch gets past 90 degrees:
trc45z