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 KaustavDG123 · Oct 29, 2018 at 07:55 PM · quaternioneuleranglesclampclamped rotationclampforever

Mathf.Clamp is not clamping my minimum value

Mathf.Clamp is resetting my eulerangle to maxValue when it reaches the minValue instead of constraining it at minValue. My Script

             rotate = Input.GetAxis("Mouse X") * senseX;
             rotation = player.transform.eulerAngles;
             rotation.y += rotate * RateOfRotate;
 
             //while (rotation.y > 180)
            // {
            //     rotation.y -= 360;
         //    }
             rotation.y = Mathf.Clamp(rotation.y, lowLimitY, highLimitY);
             player.transform.eulerAngles = rotation;

Here my lowLimmitY = 0 and highLimitY = 180 ; I am stuck at this and have no clue how to work around it. Any help will be appreciated.

Comment
Add comment · Show 1
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
avatar image Nomenokes · Oct 30, 2018 at 12:42 AM 0
Share

I think it's because eulerAngles switches to 359 when it goes to what should be -1. Could you check on that (Debug.Log the euler angle y value)? If so, you would do something like this:

 if(rotation.y > 180) rotation.y = 360-rotation.y;
 //insert before the clamp

1 Reply

· Add your reply
  • Sort: 
avatar image
4
Best Answer

Answer by JVene · Oct 30, 2018 at 02:56 AM

@Nomenokes is right, but I thought I'd expand on it a touch.


Clamp isn't actually appropriate for angles. What's required should be more accurately called a wrap, the way a clock face wraps from noon to 1 after touring from 2 through 11, or from 59 to 0 when referencing seconds or minutes. Angles can't be limited to a range of 0 to 180, as that would only be half the potential rotation. The extent of the range must be 360, for degrees (2 * PI for radians), but that could be from 0 to 359, wrapping to 0, or -180 to 180 (where wrapping is actually -179.999... to 179.999..., the fact is that -180 and 180 are the same if that is the range used.


Some input sources you may know only give you a partial rotation, and as such a wrap can be simple. However, a fully generalized wrap may have to contend with multiple rotations, resolving the result of multiple rotations into the correct range. I'll leave the latter for a search you can perform, but consider the case when your target rage is (in reality) -180 to 180, and you know only partial rotations will ever be applied (nothing approaching multiple rotations). In that case, something like this is appropriate:

 if ( angle > 180 ) angle -= 360;
 if ( angle <= -180) angle += 360;

Note the second version uses "less than or equal", which is optionally intended to ensure that -180 becomes 180 (which are the same thing, and thus there can't be two very different looking results that resolve to the same angle).

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

96 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

Related Questions

Bizzare problem with limiting camera veritcal rotation 2 Answers

Clamp camera quaternion so you can't look up or down more than 90 degrees? 3 Answers

Edited mouselook script rotation clamp not working 0 Answers

Clamped Rotate 3D Object on Y and Z axis only with OnMouseDrag() 2 Answers

Limit Rotation of physics object in 2D 1 Answer


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