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 Flux · Jun 21, 2013 at 07:17 PM · javascripttouchtexture2dangledegrees

Rotate 2D Texture negative positive degrees touch

Hello,

I am working on a kind of wheel which you can rotate 360 degrees with your fingers. The positive degrees are working nicely. But the problem is, how can i make it, so when your finger goes to the left, the degrees goes negative?

Thanks in advance, Flux

Code:

 public var texture : Texture2D = null; 
 public var size : Vector2 = new Vector2(128, 128); 
 
 private var angle : float = 0; 
 private var pos : Vector2 = new Vector2(0, 0); 
 private var rect : Rect; 
 private var pivot : Vector2; 
 private var rotating : boolean = false; 
 private var initialMouseAngle : float; 
 
 function Start() { 
     UpdateSettings(); 
 } 
 
 function UpdateSettings() { 
    pos = new Vector2(180, Screen.height - texture.height + 350); 
    rect = new Rect(pos.x - size.x * 0.5f, pos.y - size.y * 0.5f, size.x, size.y); 
    pivot = new Vector2(rect.xMin + rect.width * 0.5f, rect.yMin + rect.height * 0.5f);
 } 
 
 function OnGUI() { 
    if (Application.isEditor) { UpdateSettings(); } 
     
     
    if(Input.touchCount > 0) {  
         var touch : Touch = Input.GetTouch(0); 
         var guiMouse : Vector2 = Vector2(touch.position.x, touch.position.y); 
         guiMouse.y = Screen.height - guiMouse.y; 
          
         if ((touch.phase == TouchPhase.Began) && rect.Contains(guiMouse)) { 
          var v2T : Vector2 = (guiMouse - pivot); 
          initialMouseAngle = Mathf.Atan2(v2T.y, v2T.x) - angle * Mathf.Deg2Rad; 
          rotating = true; 
         }
         else if ((touch.phase == TouchPhase.Moved) & rotating) { 
          var v2T2 : Vector2 = (guiMouse - pivot); 
          angle = (Mathf.Atan2 (v2T2.y, v2T2.x) - initialMouseAngle)  * Mathf.Rad2Deg;     
          if(angle < 0) {
             angle += 360;  
              Debug.Log(angle);
          }
         } 
         else if ((touch.phase == TouchPhase.Ended)) { 
          rotating = false; 
          angle = 0; 
         }
    } 
     
     angle = Mathf.Clamp(angle, -360, 360);
     GUI.Label(Rect(100, 30, 100, 30), angle.ToString());
     
     var matrixBackup : Matrix4x4 = GUI.matrix; 
     GUIUtility.RotateAroundPivot(angle, pivot); 
     GUI.DrawTexture(rect, texture); 
     GUI.matrix = matrixBackup; 
 }  
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 Flux · Jun 22, 2013 at 12:56 PM 0
Share

Anyone? I tryed a lot of things, but nothing worked!

avatar image RalphTrickey · Jun 22, 2013 at 02:36 PM 0
Share

Is the issue that the display isn't rotating, or that the touch isn't showing a negative rotation?

avatar image Flux · Jun 22, 2013 at 04:35 PM 0
Share

The issue is that it doesn't show a negative rotation value!

avatar image RalphTrickey · Jun 22, 2013 at 08:05 PM 0
Share

Just to make sure it isn't something silly that I might do and to understand the question...

This ensures it will never be negative. if(angle < 0) { angle += 360;
Debug.Log(angle);

avatar image RalphTrickey · Jun 23, 2013 at 12:37 AM 1
Share

That's weird. The only thing I can see is that it says that OnGui can be called multiple times per frame. Can you move that whole thing to the Update() method ins$$anonymous$$d and see if that helps? Aside from that, I'm out of ideas.

Show more comments

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by Dolkar · Jun 22, 2013 at 10:53 PM

Try setting the angle to angle % 360. The reported number will always be between 0 and 360, but the wheel should move as expected.

Comment
Add comment · Show 16 · 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 Flux · Jun 23, 2013 at 09:54 AM 0
Share

Thanks for your response, but that also didn't work! The problem it's the same, when i hit the -90 degrees, it goes to 270 degrees.

avatar image Dolkar · Jun 23, 2013 at 12:54 PM 0
Share

And how is that a problem?

avatar image Flux · Jun 23, 2013 at 12:58 PM 0
Share

Well, the object is a kind of wheel. When i rotate my finger to the left around the wheel, the degrees goes negative until -90 degrees. Then the degrees goes positive, 270 degrees. It works fine when i rotate my finger to the right around the wheel, the degrees goes then to 360 degrees positive. So, the negative degrees are not working properly, i want that the negative degrees goes to -360 degrees.

avatar image Dolkar · Jun 23, 2013 at 02:25 PM 0
Share

And when it reaches these boundaries you want the wheel to stop or wrap around?

avatar image Flux · Jun 23, 2013 at 04:23 PM 0
Share

Then i want the wheel to stop!

Show more comments
avatar image
0

Answer by RakeshSingh · Jul 20, 2013 at 02:55 PM

remove this form ur code......... if(angle < 0) { angle += 360;

Comment
Add comment · 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

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

18 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

Related Questions

2D Rotation Lerp Back To 0 Degrees, Please Help 2 Answers

Detecting Multiple Touches on same frame IOS 2 Answers

How to cut an image at an angle from the center? 0 Answers

put a range between two degrees into code ? see image . 2 Answers

Touch drag gameObject with any camera angle 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