Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
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
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges