Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 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 /
  • Help Room /
avatar image
0
Question by JDCAce55 · Mar 05, 2019 at 06:23 AM · rotationquaternioneuler angles

How to clamp rotation, following Unity's quaternion rules

I want to clamp my camera's pitch (rotation around its x-axis), and nearly every forum post or Unity Answers page says to use this method:

  1. Grab the Euler angle for the angle to be clamped.
  2. Clamp using Math.Clamp().
  3. Assign the new angle to the rotation.


However, this method is precisely what Unity advises not to do in their Quaternion and Euler Rotation article.


After some more searching, I found another answer that points to Unity's own MouseLook.cs script in the Standard Assets. This script uses the following function (full code in the link):

 Quaternion ClampRotationAroundXAxis(Quaternion q)
 {
     q.x /= q.w;
     q.y /= q.w;
     q.z /= q.w;
     q.w = 1.0f;
  
     float angleX = 2.0f * Mathf.Rad2Deg * Mathf.Atan (q.x);
     angleX = Mathf.Clamp (angleX, MinimumX, MaximumX);
     q.x = Mathf.Tan (0.5f * Mathf.Deg2Rad * angleX);
  
     return q;
 }


I've implemented this method in my own code, with a slight alteration explained in the comments in the code.

 public void Rotate(Vector3 input)
 {
     // The camera's pitch (xRotation) should be done about its own x-axis (Space.Self),
     // but its pan (yRotation) should be down about the world's y-axis (Space.World).
     // In order to accomplish this, I need to rotate the camera twice, isolating the x- and y-rotations.
     Vector3 xRotation = new Vector3(input.x, 0, 0);
     Vector3 yRotation = new Vector3(0, input.y, 0);
     transform.Rotate(xRotation, Space.Self);
     transform.Rotate(yRotation, Space.World);
 
     // Clamp the xRotation amount - Bad way
     //Vector3 currentRotation = transform.rotation.eulerAngles;
     //currentRotation.x = Mathf.Clamp(currentRotation.x, pitchMin, pitchMax);
     //transform.rotation = Quaternion.Euler(currentRotation);
 
     // Clamp the xRotation amount - Good way
     transform.rotation = ClampRotationAroundXAxis(transform.rotation);
 }
 
 // This method is taken in full from Unity's MouseLook.cs class in the Standard Assets.
 private Quaternion ClampRotationAroundXAxis(Quaternion q)
 {
     q.x /= q.w;
     q.y /= q.w;
     q.z /= q.w;
     q.w = 1.0f;
 
     float angleX = 2.0f * Mathf.Rad2Deg * Mathf.Atan(q.x);
     angleX = Mathf.Clamp(angleX, pitchMin, pitchMax);
     q.x = Mathf.Tan(0.5f * Mathf.Deg2Rad * angleX);
 
     return q;
 }

However, an odd behavior occurs when I try to rotate the camera beyond the clamp: the camera begins to roll about its z-axis. (GIF included below.) What's going on here, and how do I fix it? Is it caused by my two-rotation solution?

Issue in action

unityrotationissue.gif (297.9 kB)
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 gasparweb · May 30, 2020 at 06:57 AM

The best answer I found is this:

https://www.reddit.com/r/Unity3D/comments/3jj2p5/clamping_angle_of_quaternion_issue/

also check this:

https://www.youtube.com/watch?v=bCz7awDbl58

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

200 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 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

I can get Quaternion data(x,y,z,w) from simulation program. But, how to assign it in rotation? 1 Answer

Mirror rotation of object along 2 Euler Axis' 0 Answers

transform.rotation spazes out 0 Answers

Mobile Gyroscope, make Camera always rotating towards zero point using Quaternion 2 Answers

Using a directional vector to orient a sphere? 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