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 AndreasX12 · Jul 02, 2013 at 10:07 AM · rotationguitexturerotatelimit

Limit GUI Rotation?

Hello, I have this script which rotates a gui (In my case, a steering wheel). I'am trying to limit the angle from -70 degress to 70 degress. I have done that by using Mathf.Clamp, which works perfectly. The only problem is, that when my mouse's Y position comes under the pivotPoint's Y position, the angle = 70. Here's the code: (Just create an empty C# script, attach it to a game object, assign a texture and try to rotate the gui)

Thanks, Andreas :-)

     public Texture2D texture = null;
     public Vector2 GUIsize;
     
     public Vector2 v2T;
  
     public float angle = 0;
     private Vector2 pos = new Vector2(0,0);
     private Rect rect;
     private Vector2 pivot;
     private bool rotating = false;
     private bool drawAngle = true;
     public float initialMouseAngle;
  
     void Start() {
         UpdateSettings();
     }
  
     void UpdateSettings() {
         GUIsize = new Vector2((128 * Screen.width / 1000) + (128 * Screen.height / 1000) , (128 * Screen.width / 1000) + (128 * Screen.height / 1000));
            pos = new Vector2(Screen.width, Screen.height);
         rect = new Rect(pos.x - GUIsize.x, pos.y - GUIsize.y , GUIsize.x, GUIsize.y);
            Debug.Log ("Rect="+rect);
         pivot = new Vector2(rect.xMin + rect.width / 2, rect.yMin + rect.height / 2);
     }
  
     void OnGUI() {
         
 
         
         if (Application.isEditor) { 
             UpdateSettings(); 
         }
         
         Vector2 guiMouse = Input.mousePosition;
         
         guiMouse.y = Screen.height - guiMouse.y;
         
            if (Input.GetMouseButtonDown(0) && rect.Contains(guiMouse)) {
              v2T = ((Vector2)guiMouse - pivot);
              initialMouseAngle = Mathf.Atan2(v2T.y, v2T.x) - angle * Mathf.Deg2Rad;
              rotating = true;
            }
         
            else if (Input.GetMouseButton(0) & rotating) {
              v2T = ((Vector2)guiMouse - pivot);
              angle = (Mathf.Atan2 (v2T.y, v2T.x) - initialMouseAngle)  * Mathf.Rad2Deg;
            }
                
         else if (Input.GetMouseButtonUp(0)) {
             rotating = false;
            }
         
         
         angle = Mathf.Clamp (angle, -70, 70);
         
         if(drawAngle == true) {
             
             Matrix4x4 matrixBackup = GUI.matrix;
             GUIUtility.RotateAroundPivot(angle, pivot);
             GUI.DrawTexture(rect, texture);
             GUI.matrix = matrixBackup;
             
         }
         
         
     }
     
     
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

2 Replies

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

Answer by Immanuel-Scholz · Jul 02, 2013 at 01:16 PM

MH... Idea: In line 36, instead of using the "correct" gui position, just clamp the position to the upper screen.

 guiMouse.y = 0;
Comment
Add comment · Show 4 · 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 AndreasX12 · Jul 02, 2013 at 04:46 PM 0
Share

Hmmm, that doesn't seem to help either. I have found out, that it's because the angle is not going from 0-360 but it's actually changing the angle to different values. It can be negative and positive numbers and it seems to be changing whenever the mouse/gui has moved 270 degrees. I don't know what can be causing it.

Thanks, Andreas.

avatar image Immanuel-Scholz · Jul 02, 2013 at 07:11 PM 0
Share

because the angle is not going from 0-360

Using Atan2, I would have expected the angle goes from -PI/2 to PI/2, (-180 to 180).

So yea, your trail is good. You probably have some hickup when subtracting the angles from each other and you need somewhere an "`if (angle < 0) angle += 360`".

Can't figure out exactly where, though. These angle stuff always gives me headaches :( ;)

avatar image AndreasX12 · Jul 02, 2013 at 07:59 PM 0
Share

Thank you :-) I have tried for hours of hours now, but I have still not found out how to do it.. If you have more ideas about what could solve it, I would be really happy :-)

avatar image AndreasX12 · Jul 03, 2013 at 07:49 AM 0
Share

Thank you :) I have figured it out now by converting the angle value to a 0-360 degress angle with this:

angle = -angle; angle = 360 - angle;

It works perfectly now :)

avatar image
0

Answer by Jamora · Jul 03, 2013 at 02:45 AM

If the problem is that the mouse going under the pivot point's y coordinate causes unintended behavior. Try something like this:

     if(guimouse.y<pivot.y)
         angle = Mathf.Clamp (angle, -70, 70);

Note that Input.mouseposition has an inverted y-coordinate; bottom = 0, while OnGUI uses top = 0. But since you have it working, you've already accounted for this, somehow.

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 AndreasX12 · Jul 03, 2013 at 07:49 AM 0
Share

Thank's for your answer, Jamora. I have figured it out now by converting the angle value to a 0-360 degress angle with this:

angle = -angle; angle = 360 - angle;

It works perfectly now :)

avatar image Milot · Feb 22, 2015 at 09:59 AM 0
Share

Hi Andreas, have you solved this problem? I have the same problem, trying for hours.. If you have found a solution please show how to limit the angle if mouse is going under pivot y

avatar image AndreasX12 · Feb 22, 2015 at 07:33 PM 0
Share

Hi $$anonymous$$ilot. I looked into my old scripts and it seems like i added this code before line 54 (angle = $$anonymous$$athf.Clamp (angle, -70, 70);):

         if(angle > 360) {
             angle = 0;    
         }
         
         if(angle < -80) {
             angle = -angle;
             angle = 360 - angle;
         }

Let me know if it works :)

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

17 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

Related Questions

Limiting rotation of object, specifically using scroll wheel 2 Answers

How limit the rotation of an object Or if there is other way to rotate 1 Answer

GUIUtiliy.RotateAroundPivot() doesn't work 0 Answers

Rotating a Rotated GUI Texture 0 Answers

Limit Rotations with Min and Max on Mobile 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