Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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
1
Question by musoufan91 · Aug 26, 2013 at 10:01 PM · rotationinputinterface

Rotate A dial with circular motions

This has been the bane of my existence for the past 2 weeks and have not been able to get it to do what I want. I have the dial turning correctly, But using the mouse or touch is not very intuitive. I can turn the dial by dragging the mouse to the left or right on the horizontal. but when I try and make circular motions it does not work.

Any help would be appreciated.

Comment
Add comment · Show 2
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 Jamora · Aug 26, 2013 at 10:04 PM 1
Share

Is this a GUI object (called from OnGUI) or an object in 3D space?

avatar image musoufan91 · Aug 27, 2013 at 01:09 AM 0
Share

The dial is in 3d space

2 Replies

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

Answer by musoufan91 · Aug 30, 2013 at 01:40 AM

Solution if object is rotating on the Z. Most of the credit goes to robertbu.

 private Quaternion originalRotation;
 private float  startAngle = 0;
 
 public void Start()
 {
         originalRotation = this.transform.rotation;
         
 }
 
 public void InputIsDown()
 {
 #if UNITY_IPHONE || UNITY_ANDROID
         
         originalRotation = this.transform.rotation;
         Vector3 screenPos = Camera.main.WorldToScreenPoint(transform.position);
         Vector3 vector = Input.GetTouch(0).position - screenPos;
         startAngle = Mathf.Atan2(vector.y, vector.x) * Mathf.Rad2Deg;
         //startAngle -= Mathf.Atan2(transform.right.y, transform.right.x) * Mathf.Rad2Deg;  // uncomment to pop to where mouse is 
 
 #else
         
         originalRotation = this.transform.rotation;
         Vector3 screenPos = Camera.main.WorldToScreenPoint(transform.position);
         Vector3 vector = Input.mousePosition - screenPos;
         startAngle = Mathf.Atan2(vector.y, vector.x) * Mathf.Rad2Deg;
         //startAngle -= Mathf.Atan2(transform.right.y, transform.right.x) * Mathf.Rad2Deg;  // uncomment to pop to where mouse is 
 
 #endif
 }
 
 public void InputIsHeld()
 {
 #if UNITY_IPHONE || UNITY_ANDROID
 
         Vector3 screenPos = Camera.main.WorldToScreenPoint(transform.position);
         Vector3 vector = Input.GetTouch(0).position - screenPos;
         float angle = Mathf.Atan2(vector.y, vector.x) * Mathf.Rad2Deg;
         Quaternion newRotation = Quaternion.AngleAxis(angle - startAngle , this.transform.forward);
         newRotation.y = 0; //This and the line below, may need to be changed depending on what axis the object is rotating on. 
         newRotation.eulerAngles = new Vector3(0,0,newRotation.eulerAngles.z);
         this.transform.rotation = originalRotation *  newRotation;
     
 #else
         Vector3 screenPos = Camera.main.WorldToScreenPoint(transform.position);
         Vector3 vector = Input.mousePosition - screenPos;
         float angle = Mathf.Atan2(vector.y, vector.x) * Mathf.Rad2Deg;
         Quaternion newRotation = Quaternion.AngleAxis(angle - startAngle , this.transform.forward);
         newRotation.y = 0; //see comment from above 
         newRotation.eulerAngles = new Vector3(0,0,newRotation.eulerAngles.z);
         this.transform.rotation = originalRotation *  newRotation;
         
         
 #endif
 }


Comment
Add comment · Show 1 · 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 Clayburn · Jun 24, 2015 at 05:43 PM 1
Share

How do you figure this works?

avatar image
2

Answer by robertbu · Aug 26, 2013 at 11:06 PM

I've answered this question several times with different solutions for both GUI.DrawTexture() and for object in 3D space. Here is one of those links:

http://answers.unity3d.com/questions/389586/rotate-gui-texture-by-touch.html

Comment
Add comment · Show 2 · 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 robertbu · Aug 27, 2013 at 03:46 AM 0
Share

The solution for an object in world space will depend on the natural orientation of the object. Here is a solution that works when the camera is looking at the back of the object. In particular it works with a plane created by the Unity Wiki CreatePlane editor script with the plane in vertical orientation:

 #pragma strict
 
 var startAngle = 0.0;
 
 function On$$anonymous$$ouseDown() {
     var screenPos = Camera.main.WorldToScreenPoint(transform.position);
     var vec = Input.mousePosition - screenPos;
     startAngle = $$anonymous$$athf.Atan2(vec.y, vec.x) * $$anonymous$$athf.Rad2Deg;
     startAngle -= $$anonymous$$athf.Atan2(transform.right.y, transform.right.x) * $$anonymous$$athf.Rad2Deg;
 }
 
 function On$$anonymous$$ouseDrag() {
     var screenPos = Camera.main.WorldToScreenPoint(transform.position);
     var vec = Input.mousePosition - screenPos;
     var angle = $$anonymous$$athf.Atan2(vec.y, vec.x) * $$anonymous$$athf.Rad2Deg;
     transform.rotation = Quaternion.AngleAxis(angle - startAngle , Vector3.forward);
 }

avatar image musoufan91 · Aug 29, 2013 at 03:28 AM 0
Share

Thank you. When I added it in the dial became backwards but worked. I fiddled with it and if you add

 public void Start()
     {
             
         originalRotation= this.transform.rotation;
     }

then

 this.transform.rotation = originalRotation * Quaternion.AngleAxis(angle - startAngle , this.transform.forward);

It works.

I am going to modify it for touch then post it here so others can grab it.

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

16 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

Related Questions

How to rotate sphere when moving in Unity 1 Answer

Shooting in direction of the character movement 2 Answers

Flip over an object (smooth transition) 3 Answers

How to rotate with mouse scroll wheel and new input system? 1 Answer

Rotation of character resets when joystick is released 3 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