title: Adjust camera orientation through code (Unity3D)
body: <p>I posted this same question on the Unity Ask forums, but I'm not getting a response so I hope someone here will help.  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 <a href="http://docs.unity3d.com/ScriptReference/Transform.RotateAround.html" rel="nofollow">Transform.RotateAround</a>) around a point within that mountain. The camera also needs to pan right and left, which I do by <a href="http://docs.unity3d.com/ScriptReference/Transform.Translate.html" rel="nofollow">Translating it on its local left and right vectors</a>. However, once you use enough of Translate and RotateAround, the camera becomes tilted to the side, and it gets progressively worse as you play.</p>
  
  <p>I have tried correcting it by running this function every frame:</p>
  
  <pre><code>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);
   }
  </code></pre>
  
  <p>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?</p>
  
  <p>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, and linked below.</p>
  
  <p>Video of the problem: <a href="https://youtu.be/J3a__u8vrYo" rel="nofollow">https://youtu.be/J3a__u8vrYo</a></p>
  
  <p>The source of the formula I am using: <a href="http://www.vitutor.com/geometry/distance/line_plane.html" rel="nofollow">http://www.vitutor.com/geometry/distance/line_plane.html</a></p>
  
  <p>Thank you for any help you may be able to provide!</p>