- Home /
Adjust camera orientation through code
I am working on a game where the camera needs to focus on a mountain at the center of an island. The camera works by rotating (using Transform.RotateAround) around a point within that mountain. The camera also needs to pan right and left, which I do by Translating it on its local left and right vectors. However, once you use enough of Translate and RotateAround, the camera becomes tilted to the side, and it gets progressively worse as you play.
I have tried correcting it by running this function every frame:
private void StraightenCamera()
{
//tilt it upward so it is always oriented right
//goal: make the local up vector lie in the plane defined by the local forward and global up vector
//http://www.vitutor.com/geometry/distance/line_plane.html
Vector3 localForward = this.transform.TransformDirection(Vector3.forward);
Vector3 globalUp = Vector3.up;
Vector3 u = this.transform.TransformDirection(Vector3.up);
Vector3 n = Vector3.Cross(localForward, globalUp);
float numerator = Mathf.Abs(n.x * u.x + n.y * u.y + n.z * u.z);
float denominator = Mathf.Sqrt(n.x * n.x + n.y * n.y + n.z * n.z) + Mathf.Sqrt(u.x * u.x + u.y * u.y + u.z * u.z);
float angle = Mathf.Rad2Deg * Mathf.Asin(numerator / denominator);
this.transform.Rotate(Vector3.forward, angle, Space.Self);
}
This function mostly works, however there is a catch. It'll rotate the camera to the correct orientation after a few transformations, and then it'll decide to rotate the wrong way until it corrects to be perfectly upside down from where it should be. Mess around with it a little bit more and it'll correct itself to the right orientation, but as you continue to play it'll get confused and flip upside down again. I don't understand what is triggering the flipping, or where the problem in my math is. Does anyone see it?
Explanation of the above function: My thinking was that as it reorients itself, its local up vector was getting tilted. I don't care where it points, so long as it is in the plane defined by the local forward vector (wherever the player may have pointed that), and the global up vector. I am rotating the camera around its local forward axis in an attempt to maintain this condition. One hint that my math is off is that it doesn't correct this misalignment in a single frame like I intend, but it does so gradually, either when it is correcting to the correct orientation or upside down. The website I got the formula from is in a comment in the function.
Video of the problem: https://youtu.be/J3a__u8vrYo
Thank you for any help you may be able to provide!
I almost never comment on Rotation questions because I'm hopeless with those, but one note from LateUpdate() doc says use it for Camera stuff, so maybe try calling this from LateUpdate() assu$$anonymous$$g it's currently in Update().
http://docs.unity3d.com/ScriptReference/$$anonymous$$onoBehaviour.LateUpdate.html
I tried it out but it made no difference. Thanks for the suggestion though.
Your answer
Follow this Question
Related Questions
Camera Clamping Third Person? 0 Answers
Multiple Cars not working 1 Answer
Lean/Peak system 1 Answer
Confining Mouse Look on X axis 0 Answers