- Home /
 
               Question by 
               DarthRayban · May 08, 2016 at 10:46 AM · 
                camerarotationscripting problemcamera rotate  
              
 
              View Matrix Camera rotation problem
Hi, i need to rotate a camera around a pivot point. I'm using a rotation matrix to modify the camera view matrix, and this works fine. The problem is when i get an angle from 180° to 360°, then the camera keep rotating without control. I fixed for angles from 270 to 360 but i cant find a solution for angles from 180° to 270°. What i'm doing wrong?
 using UnityEngine;
 using System.Collections;
 
 public class ViewMatrixRot : MonoBehaviour
 {
 
     public float c = 0f; //angle
     float a=0f,b=0f,asin=0f;
     
     // Update is called once per frame
     void Update ()
     {
         if (c < 0) c += 360f;
         Matrix4x4 vm = GetComponent<Camera> ().worldToCameraMatrix;
         if (c >= 180f)
         {
             if (c < 270f)
             {
                 float acos = Mathf.Acos (vm [2, 2]);
                 b = Mathf.Rad2Deg * acos;
                 a = c - b;
             }
             asin = Mathf.Asin (vm [0, 2]);
             if (asin < 0)
                 asin += (2 * Mathf.PI);
             b = Mathf.Rad2Deg * asin;
             a = c - b;
     
         }
         else
         {
             b = Mathf.Rad2Deg * Mathf.Acos (vm [0, 0]);
             a = c - b;
         }
 
         Matrix4x4 rot = Matrix4x4.zero;
         rot [0, 0] = Mathf.Cos(Mathf.Deg2Rad*a);  rot [0, 1] = 0; rot [0, 2] = Mathf.Sin(Mathf.Deg2Rad*a);  rot [0, 3] = 0;
         rot [1, 0] = 0;                              rot [1, 1] = 1; rot [1, 2] = 0;                             rot [1, 3] = 0;
         rot [2, 0] = -Mathf.Sin(Mathf.Deg2Rad*a); rot [2, 1] = 0; rot [2, 2] = Mathf.Cos(Mathf.Deg2Rad*a);  rot [2, 3] = 0;
         rot [3, 0] = 0;                           rot [3, 1] = 0; rot [3, 2] = 0;                            rot [3, 3] = 1;
     
         Transform p = GameObject.Find ("WorldSystem").GetComponent<Transform> ();
         float px = p.transform.position.x;
         float py = p.transform.position.y;
         float pz = p.transform.position.z;
 
         Matrix4x4 pivot = Matrix4x4.zero;
         pivot [0, 0] = 1; pivot [0, 1] = 0; pivot [0, 2] = 0; pivot [0, 3] = px;
         pivot [1, 0] = 0; pivot [1, 1] = 1; pivot [1, 2] = 0; pivot [1, 3] = py;
         pivot [2, 0] = 0; pivot [2, 1] = 0; pivot [2, 2] = 1; pivot [2, 3] = pz;
         pivot [3, 0] = 0; pivot [3, 1] = 0; pivot [3, 2] = 0; pivot [3, 3] = 1;
 
         Matrix4x4 np = Matrix4x4.zero;
         np [0, 0] = 1; np [0, 1] = 0; np [0, 2] = 0; np [0, 3] = -px;
         np [1, 0] = 0; np [1, 1] = 1; np [1, 2] = 0; np [1, 3] = -py;
         np [2, 0] = 0; np [2, 1] = 0; np [2, 2] = 1; np [2, 3] = -pz;
         np [3, 0] = 0; np [3, 1] = 0; np [3, 2] = 0; np [3, 3] = 1;
 
         Matrix4x4 net = np * rot;
 
         Matrix4x4 rm = net * pivot;
 
         GetComponent<Camera> ().worldToCameraMatrix = rm*vm;
         vm = GetComponent<Camera> ().worldToCameraMatrix;
 
         Debug.Log ("----------------------------------------------------------");
         Debug.Log ("a: "+a+" b: "+b+" asin: "+asin);
         Debug.Log ("----------------------------------------------------------");
 
 
     
     }
 }
               Comment
              
 
               
              Your answer
 
 
             Follow this Question
Related Questions
FPS Camera Control rotation limits 1 Answer
[Solved :D] Camera auto turns 180° from its init position on Play (Animated GIF Included!) 1 Answer
Reset player movement axes after camera rotation 1 Answer
rotateAround pivots at unexpected point 1 Answer
How to make the skybox pitch up and down with the camera? 0 Answers
 koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                