Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 sagicoh · Nov 11, 2021 at 08:45 PM · rotationcamera-movementrotation axis

Wrong camera rotation value

I'm rotating the main camera around an object. for now only on the X axis. But I see some wierd values for the camera's rotaion. As I rotate it, I see the camera's rotation (Camera.main.transform.rotation.eulerAngles) goes up to 90 and then it goes back to 0, after 0 it suddenly jumps to 360 and continues to confuse me. also axis z and axis y change to 180 at some point

this is the code for the camera movement:

 private void CameraMovement()
 {
     foreach (var touch in Input.touches)
     {
         if (touch.phase == TouchPhase.Began)
         {
             _previousPosition = _cam.ScreenToViewportPoint(touch.position);
         }
         else if (touch.phase == TouchPhase.Moved)
         {
             OnMouseMove(touch.position);
         }
     }
 
     if (Input.GetMouseButtonDown(0))
     {
         _previousPosition = _cam.ScreenToViewportPoint(Input.mousePosition);
     }
     else if (Input.GetMouseButton(0))
     {
         OnMouseMove(Input.mousePosition);
     }
 }
 
 private void OnMouseMove(Vector3 moveTo)
 {
     Vector3 newPosition = _cam.ScreenToViewportPoint(moveTo);
     Vector3 direction = _previousPosition - newPosition;
 
     _cam.transform.position = new Vector3(0, 0, 0);
 
     _cam.transform.Rotate(new Vector3(1, 0, 0), direction.y * 180);
 
     _cam.transform.Translate(new Vector3(0, 0, -distanceToTarget));
 
     _previousPosition = newPosition;
 }

Can anyone please explain to me why and how does it happen?

thanks!

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by bdubbert · Nov 11, 2021 at 11:08 PM

Rotations and orientations are represented in Unity with Quaternions. Quaternions work very elegantly as rotations and make manipulating them very easy. It is also worth noting that for any given rotation, there is exactly one corresponding Quaternion. However, Quaternions are mathematical mumbo jumbo to 99% of the population. There is a more human-readable way to represent rotations called Euler Angles. These are just the rotations about each axis you would need to do to get to the desired rotation. However Euler Angles suffer from several mathematical setbacks, one of which being that for any given rotation there are infinite euler angle representations. For example (0, 0, 0) represents the same rotation as (360, 0, 0). This is also true for more complex euler angles, for example (421, 74, -150) = (119, -106, 30).


So when you ask Unity for the euler angles for a rotation, it will calculate the single simplest euler representation. However as you rotate your object around, the simplest euler representations between two adjacent rotations may not resemble eachother at all. This is where you get all of your crazy jumping numbers, you are transitioning between rotations whose simplest euler angles don't look very close to eachother even though the corresponding rotations are very close to eachother.


If you want to calculate euler angles that change smoothly with rotation there are ways to do that, but Unity only knows to give you the simplest euler angles when you ask.

Comment
Add comment · Share
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

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

182 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How do I get my camera to rotate around the y=0 coordinate it's looking at? 0 Answers

Swipe angle and Rotation of camera to movement 1 Answer

rotating objects around collider axis 1 Answer

Rotate character with camera ?! 1 Answer

Rotation gameobject 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