- Home /
Top down camera always facing north - avoid residual rotation
Hi
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.
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)?
Thanks!
Answer by MERCURY_GAME_CREATOR · Nov 08, 2016 at 10:41 AM
Make the camera a child of the racer. Hope it helps ;)
I don't think it would be suitable since the camera moves independently from the racer, and has to be restrained so if the vehicle is rolling over, the camera wouldn't make weird things.
But thanks anyway it gaves me an idea I'll try something :)
Your answer
Follow this Question
Related Questions
Quaternions; how do I rotate an object around only 2 axis based on another rotation? 1 Answer
Rotating a camera and his object like an airplane simulator 2 Answers
Top-Down Camera: Always looking where the player is facing? 1 Answer
Rotate object instead of camera around it? 0 Answers
How do I get my camera to rotate around the y=0 coordinate it's looking at? 0 Answers