Camera Rotation Flipping with Quaternion - Gimbal Lock

I am changing views by camera , single axis change is coming good, but when i am doing combined change with x and y axis, when its reached x =90 and y = 90 or some other value, its affected with Gimbal Lock and y axis is Flipped and return to zero (y = 0), the code i am using is below:

t += Time.deltaTime * changeStep;

Vector3 origin = new Vector3 (0f, 0f, 0f);
maincamera.transform.rotation = Quaternion.Slerp(maincam.transform.rotation, newrot, t);
maincamera.transform.position = Vector3.Slerp(startingPos, targetPos, t);
maincamera.transform.LookAt (origin);
i used several ways, but same problem persists.

Not sure what you mean by changing views. This comment may not be useful… but here it is:

I really enjoy a 3-part camera setup. first part rotates the ‘x’ axis, its child object rotates the (local) ‘y’ axis, and (if needed), the 3rd child (the camera) can move in/out on the local z -axis.

Changing camera from front view to top view
For example
frontview position = 0,1,-20
topview position = 0,20,0
topvew rotation = 90,90,0
when i am using the above code, the y value will be flipped to zero
topview rotation will become 90,0,0

any suggestions?

Not sure. Try the 3 part camera setup to see if it helps.
Design like so:
Rig (empty game object) : only rotates y-axis

  • child Pivot (empty game object) : only rotates on local x-axis
  • child of Pivot (Camera)

use camera local z-axis to zoom in/out.
set camera (Rig) position to that of the object you’re viewing.
Do not use ‘LookAt’ , as that should be… automatic with this setup.

Hope that helps. :slight_smile:

i will try that… thanks