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 keiranlovett · Nov 20, 2014 at 05:01 AM · ui4.6

Unity 4.6 - Rotating with Mouse Issue

I am trying to create a "dial" element, which can be used for various cases in Unity 4.6 UI. I have the dragging working, however for some reason, the rotation following the mouse is difficult, not letting the mouse rotate the button around a full 360.

 public GameObject texture = null;
     
      private float angle = 0;
      private Vector2 pos = new Vector2(0, 0);
      private Rect rect;
      private Vector2 pivot;
      private bool rotating = false;
      private float initialMouseAngle;
      
      void Start() {
          rect = new Rect(texture.transform.localPosition.x - texture.GetComponent<RectTransform>().sizeDelta.x * 0.5f, texture.transform.localPosition.y - texture.GetComponent<RectTransform>().sizeDelta.y * 0.5f, texture.GetComponent<RectTransform>().sizeDelta.x, texture.GetComponent<RectTransform>().sizeDelta.y);
          pivot = new Vector2(rect.xMin + rect.width * 0.5f, rect.yMin + rect.height * 0.5f);
      }
   
        void Update() {
          Vector2 guiMouse = Input.mousePosition;
 
          if (Input.GetMouseButtonDown(0)) {
              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;
          }
          else if (Input.GetMouseButtonUp(0)) {
              rotating = false;
          }
 
           texture.transform.rotation = Quaternion.AngleAxis(angle, Vector3.forward);
 
      }
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

0 Replies

· Add your reply
  • Sort: 

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

2 People are following this question.

avatar image avatar image

Related Questions

Showing text on top of a Slider, but not blocking Handler? 1 Answer

Android screen is small? 0 Answers

4.6 Button OnClick Super Sensitive to pointer movement. 1 Answer

Unity 4.6 - Detect Touch On Screen 1 Answer

OnPointerExit doesn't work 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