Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 AndreasC · Apr 25, 2014 at 12:16 PM · clamped rotation

Mathf.Clamp not working as expected with rotation values

Hey !

I'm having a annoying issue with Mathf.Clamp. The issue is that I can't clamp the rotation between 330 and 30. This script below is attached to a gun held by a character. I want to basicly limit the rotation between 330 and 30. I've tried limiting the angle in between 30 and 330 but then the gun shoots backwards.

I hope you guys could help me out, and sorry about my shitty explanation.

 void Update ()
     {
         Vector3 mousePos = Input.mousePosition;
         mousePos.z = -(transform.position.x - Camera.main.transform.position.x);
         
         Vector3 objectPos = Camera.main.WorldToScreenPoint (transform.position);
         mousePos.x = mousePos.x - objectPos.x;
         mousePos.y = mousePos.y - objectPos.y;
         
         float angle = Mathf.Atan2(mousePos.y, mousePos.x) * Mathf.Rad2Deg;
         angle += 180f; //So the gun is directed towards the mouse
         /*if(angle < 330)
         {
             transform.rotation = Quaternion.Euler(new Vector3(0, 0, angle));
         }*/
         //angle = Mathf.Clamp(angle, 330, 30);
         //angle = Mathf.Min(30);
         //angle = Mathf.Max (330);
         angle = ClampAngle (angle, 330, 30);
         //angle = Mathf.Clamp (angle, -100, 100);
         //transform.rotation = Quaternion.Euler(new Vector3(0, 0, angle));
         Debug.Log (angle);
         transform.eulerAngles = new Vector3(0, 0, angle);
     }
 
     public static float ClampAngle (float angle, float min, float max) {
         if (angle <  30.0f)
             angle += 30.0f;
         if (angle > 330.0f)
             angle -= 330.0f;
         //Debug.Log (angle);
         return Mathf.Clamp (angle, min, max);
     }
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
1

Answer by Graham-Dunnett · Apr 25, 2014 at 03:06 PM

Mathf.Clamp is documented as taking three values. The second is the min value and the third is the max value. Each time you use this API you have the min and max values reversed. I think we can all agree here that 330 is bigger than 30. I suspect what you really want is the angle to vary between -30 and +30. (Otherwise I think you'd say you want to limit between 30 and 330.) So you'd want to precondition the angle to be in the range -180..180 prior to calling Clamp(angle, -30.0f, 30.0f).

Comment
Add comment · Show 3 · 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
avatar image AndreasC · Apr 26, 2014 at 02:54 PM 0
Share

Thanks for the answer Graham ! Your answer is correct, I managed to make the gun rotation limatation working if the character is flipped to the left. Do you know how to reverse the code, so the rotation is limited on the opposite side ?

 angle = $$anonymous$$athf.Clamp (angle, -50.0f, 50.0f); //Use this if character is flipped to the right
avatar image Blamanos · Jan 02, 2018 at 01:41 PM 0
Share

I have encountered the same problem in my code, and my only problem is that mathf.clamp doesn't like negative values. So when I'm trying to limit a cameras rotation between -15 and 35 on the x axis and between -175 and -105 on the y axis. On the x axis, the rotation stops at 35, but as soon as I go over the 0 value on the x axis, it snaps to 35. As for the y axis, it is constantly locked to -105.

avatar image Lysander Blamanos · Jan 02, 2018 at 01:52 PM 0
Share

$$anonymous$$athf.Clamp works fine for negative values- your issue is in your application of it, which requires some code for people to look at. Please create a new question with the relevant code where the problem occurs (preferably boiled down to exclude everything that isn't necessary to reproduce the issue).

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

23 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

Related Questions

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

Clamp Vertical Aiming 1 Answer

Clamp issue 1 Answer

How to clamp rotation 4 Answers

Mathf.Clamp is not clamping my minimum value 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