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 iast999 · Dec 16, 2013 at 03:35 PM · guirotatemathdetectdegrees

Detect GUI rotation degree

Hi, I have a small project with genre fishing. Have a problem on detect rotation. I have a GUI texture like a reel that can be rotated.

alt text

How can i make the length decrease based on each 15 degree the reel rotated? Here is my Rotate code in C#:

 public class Rotate : MonoBehaviour {
 
     public Texture2D texture = null;
     public Vector2 size = new Vector2(0, 0);
  
     private float angle = 0;
     private Vector2 pos = new Vector2(0, 0);
     private Rect rect;
     private Vector2 pivot;
     public static bool rotating = false;
     public static bool rotating2 = false;
     private float initialMouseAngle;
     
     static float speed = 0.1f;
     
     float factor = Screen.width/800;
  
     void Start() {
         UpdateSettings();
     }
  
     void UpdateSettings() {
            pos = new Vector2(Screen.width-factor*700,Screen.height-factor*320+200);
         rect = new Rect(pos.x - size.x * 0.5f, pos.y - size.y * 0.5f, 150, 150);
            //Debug.Log ("Rect="+rect);
         pivot = new Vector2(rect.xMin + rect.width * 0.5f, rect.yMin + rect.height * 0.5f);
         //Debug.Log ("Pivot ="+pivot);
     }
  
     void OnGUI() {
            if (Application.isEditor){ 
             UpdateSettings(); 
         }
         
            Vector2 guiMouse = Input.mousePosition;
            guiMouse.y = Screen.height - guiMouse.y;
         
            if (Input.GetMouseButtonDown(0) && rect.Contains(guiMouse)) {
             Vector2 v2T = ((Vector2)guiMouse - pivot);
             initialMouseAngle = Mathf.Atan2(v2T.y, v2T.x) - angle * Mathf.Deg2Rad;
             rotating = true;
            }
            else if (Input.GetMouseButton(0) && rotating) {
              Vector2 v2T = ((Vector2)guiMouse - pivot);
             angle = (Mathf.Atan2 (v2T.y, v2T.x) - initialMouseAngle) * Mathf.Rad2Deg;
             //GetComponent<Length>().Jarak(speed);
             rotating2 = true;
             rect.ToString();
            }
            else if (Input.GetMouseButtonUp(0)) {
              rotating = false;
             rotating2 = false;
            }
         
         Matrix4x4 matrixBackup = GUI.matrix;
         GUIUtility.RotateAroundPivot(angle, pivot);
         
         if(FishingGUI.waktu == true){
             GUI.DrawTexture(rect, texture);
             animation.Play("idle");
             return;
         }
         
         GUI.matrix = matrixBackup;
     }
 }

I want to integrated with this want, so each 15 degree can decrease the length:

 public class Length : MonoBehaviour {
 
     [SerializeField]
     float MaxLength = 100f;
     float CurrentLength = 0f;
     
     void Start () {
         CurrentLength = MaxLength;
     }
     
     override public string ToString(){
         return CurrentLength + "";
     }
     
     public void Jarak(float tarikans){
         CurrentLength -= tarikans;
     }
     
     void Update(){
         if(CurrentLength <= 0){
             CurrentLength = 0;
         }
     }
 }

Please anyone can help or any other solution? Thanks

toba.png (424.0 kB)
Comment
Add comment · Show 7
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 greencvbn · Dec 16, 2013 at 04:07 PM 0
Share

There might be a way to implecate transform. into the script to make it more convinient unless c# does not use that.

avatar image iast999 · Dec 16, 2013 at 04:57 PM 0
Share

thanks for replying, but i still do not get it. Can you give me some other clue? or maybe another solution beside detecting GUI degrees?

avatar image TimBorquez · Dec 16, 2013 at 06:19 PM 2
Share

don't you already have the degree because you're using variables with RotateAroundPivot

avatar image iast999 · Dec 17, 2013 at 01:34 AM 0
Share

You mean this? But this code just make it rotate, not explain anything about degrees. Anyone please help..

         Vector2 v2T = ((Vector2)gui$$anonymous$$ouse - pivot);
         angle = ($$anonymous$$athf.Atan2 (v2T.y, v2T.x) - initial$$anonymous$$ouseAngle) * $$anonymous$$athf.Rad2Deg;
 
avatar image Vonni · Dec 31, 2013 at 05:50 AM 0
Share

$$anonymous$$aybe just grab an angle value when you start rotating. Check difference between that angle and an angle you update every frame. If it goes > or < 15 degrees make a change. If you release it at +- 6 store that and add that to your comparison next time.

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by sparkzbarca · Dec 31, 2013 at 01:59 PM

im feeling generous so here you go.

 Quanternion StoredRotation;
 
 Void Start()
 {
 StoredRotation = transform.rotation;
 }
 
 float Angle;
 float Speed = .05f;
 Void Update()
 {
 //anytime the GUI reel rotates it'll trigger this if statement
 if(transform.rotation != StoredRotation)
 {
 //get the angle
 Angle = Quanternion.Angle(transform.rotation, StoredRotation);
 
         //modify length to be equal to the angle * the speed (which can be thought of as how
 `   `many
     //meters to shorten the fishing line by per degree/radians (i'm actually not sure if
     //quaternion.angle returns in degrees or radians) of change. So if you have a speed of 2
     // it'll do 2 meters presumably per 1 degree/radian which is way high 
     //so id make it a small number.)
 
  
 LineLength = angle * speed * time.deltatime;
 } 
 }
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 iast999 · Jan 02, 2014 at 08:09 AM 0
Share

thanks for your help, but it does not work.

avatar image sparkzbarca · Jan 02, 2014 at 06:41 PM 0
Share

oh whoops

LineLength = angle speed time.deltatime;

should be

LineLength += angle speed time.deltatime;

+=

also you'll need to toss in a bool to deter$$anonymous$$e if you should add or subtract

something like

 int Line$$anonymous$$odifier;
 //let out line
 if(RotatingClockwise)
 Line$$anonymous$$odifier = 1;
 
 else
  Line$$anonymous$$odifier = -1;

LineLength += Line$$anonymous$$odifier angle speed * time.deltatime;

avatar image iast999 · Jan 03, 2014 at 07:12 AM 0
Share

i have tried many time with your code, and not work.I guess Quaternion does not work with GUI or 2D. So, any other solution sparkz?

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

22 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

Related Questions

Displaying Mathematical Equations 2 Answers

Detecting when the user starts and stop dragging a slider 1 Answer

i was wondering about gui hover... 3 Answers

Rotate 90 over time on mouseDown 2 Answers

Camera rotation around player while following. 6 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