Top down camera always facing north - avoid residual rotation


I’m making a top down racing game with a 3D perspective camera (so in a 3D world). I want my camera to face the vehicle with a restriction in angles (horizontal an vertical relative to the camera) and never rotate around its z axis (so the local up of the camera is always north).

Basicaly, when the vehicle is on a slope and align with the ground normal, the camera moves slightly to accomodate the new position, then rotate to face again the vehicle, as pictured:

Problem is, after some slopes and movements, the camera takes a little bit of rotation, so it is no longer aligned to north.

alt text

This is roughlty the code of the camera rotation (in LateUpdate):

void ManageCameraRotation() {
   Vector3 cameraDirection = (target.position - transform.position);
   Quaternion rot = Quaternion.LookRotation(cameraDirection, transform.up);
   Vector3 v = rot.eulerAngles;
   //clamping angles should be here ^^^ I think?
   Quaternion desiredRotation = Quaternion.AngleAxis(v.y, Vector3.up) * Quaternion.AngleAxis(v.x, Vector3.right) * Quaternion.AngleAxis(v.z, Vector3.forward);
   transform.localRotation = Quaternion.Slerp(transform.localRotation, desiredRotation, Time.deltaTime * 5.0f);

I don’t understand Quaternions enough to know where to lock the rotation in desiredRotation.
Or should I manually rotate the camera back to north when the vehicle is facing up (no normals)?


Make the camera a child of the racer. Hope it helps :wink: