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 graviton · Feb 24, 2014 at 04:05 PM · rotationmousetouchrotatearoundmouse-drag

Need Help, Rotating a wheel with touch or using the mouse

I have a wheel that I'm trying to rotate with touch, but not just flicking or swiping it, it has to have a circular motion

e.g.

http://www.youtube.com/watch?v=x6wOjk5AwZQ&feature=player_embedded#t=373

or

http://www.youtube.com/watch?v=1r5i4CzaPqw&feature=player_detailpage#t=38

Since I'm still prototyping I'd like to know how I could do this with the mouse (as a place holder the finger)

This has been beating me for the past few weeks, I can't seem to figure it out

Please help

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
1

Answer by robertbu · Feb 24, 2014 at 04:10 PM

You cannot rotate a GUITexture, but you can rotate either a world object or a GUI.DrawTexture() drawn object. You handle the rotation by using the angle between the mouse/finger and the object. Here is a bit of code for a world object.

 #pragma strict
 
 private var baseAngle = 0.0;
 
 function OnMouseDown() {
     var dir = Camera.main.WorldToScreenPoint(transform.position);
     dir = Input.mousePosition - dir;
     baseAngle = Mathf.Atan2(dir.y, dir.x) * Mathf.Rad2Deg;
     baseAngle -= Mathf.Atan2(transform.right.y, transform.right.x) * Mathf.Rad2Deg;
 }
 
 function OnMouseDrag() {
     var dir = Camera.main.WorldToScreenPoint(transform.position);
     dir = Input.mousePosition - dir;
     var angle =  Mathf.Atan2(dir.y, dir.x) * Mathf.Rad2Deg - baseAngle;
     transform.rotation = Quaternion.AngleAxis(angle, Vector3.forward);
 }

Since the code is using OnMouse* functions, it will work in touch as well. GUI.DrawTexture() uses GUIUtility.RotateAroundPivot() to rotate the texture.

Comment
Add comment · Show 8 · 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 graviton · Feb 24, 2014 at 05:13 PM 0
Share

robertbu

Thank you, it worked, this is what I was looking for

Quick question, I noticed that if I rotate the wheel to a certain angle and let go of the mouse press, when I press down again at a different position on the wheel, the wheel pops to it's original angle

e.g. imagine a 12 hour clock with an arrow pointing at 12, I rotate it to 2 and let go, when I press down on 6 (to continue the rotation) it pops back to 12

like the rotation is resetting every time the mouse button is pressed down, is this easily fixable (my coding is not very good)

avatar image robertbu · Feb 24, 2014 at 05:14 PM 0
Share

I edited my answer to make this change. I added line 9.

avatar image graviton · Feb 24, 2014 at 05:36 PM 0
Share

robertbu

Thank you

avatar image graviton · Mar 06, 2014 at 04:54 PM 0
Share

robertbu

I'm having a some trouble understanding some of the lines in your code The code itself works great but I've been trying to recreate it in play$$anonymous$$aker, so I thought I might be able to do it if I really understood each line of code

Sorry to be such a pain, I'm no good at Trigonometry

avatar image JaamiParvez · Jul 06, 2014 at 08:45 PM 0
Share

@robertbu .. your code works Great!! Can u please explain it a little .. what is "transform.positon" in On$$anonymous$$ouseDown and that baseAngle thing??

that would be so nice of you.. thanks :)

Show more comments
avatar image
0

Answer by Aarlangdi · Aug 18, 2015 at 04:32 AM

You do that using built in function, OnMouseDown and OnMouseDrag in very easy way. All you have to do is calculate the angle and position when you touch the object..read more here with source code... http://aarlangdi.blogspot.com.au/2014/08/rotate-object-with-mouse-click-or-touch.html

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

20 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

Related Questions

Touch mouseorbit 5 Answers

How to rotate an object around another facing to mouse? 1 Answer

How to rotate an object around a fixed point so it follows the mouse cursor? 1 Answer

Moving GameObjects with mouse and check contains 0 Answers

Scroll View scrolls much fast when dragging with touch 0 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