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 Michel Verheughe · Jan 01, 2013 at 01:04 PM · rotationmousegetaxis

Clockwise or counter-clockwise motion

I wish to animate something that can be rotated clockwise or counter-clockwise. Imagine a screw or a valve wheel that can make several turns. Because I want to enable it only from a certain distance, I use RaycastHit to highlight the instrument when accepting a mouse button down action. Currently, I use Mathf.Sign(input.GetAxis("Mouse X")) to find the direction of the rotation, then rotate the GameObject.

But it is awkward. I have to put the mouse on the top of the "wheel" to move it in the correct direction. I have been thinking of having four sectors and four Colliders but I don't think it is an elegant way to do it.

Do you have some codes that can rotate the GameObject the correct way when making a circle with the mouse?

Thanks in advance!

Michel

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

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by Owen-Reynolds · Jan 01, 2013 at 04:48 PM

Think of facing a valve. If you start above it and push right, you get clockwise. Starting below it and pushing right is counter-clockwise. Starting left of it and pushing down is counter-clockwise. Starting above and to the right and pushing right-down is clockwise. Moving your hand in a circle is just a combination of all those. So, all that matters each frame is direction to valve and last frame straight line motion.

To make it simple, do it in screen math -- find the current screen coords of the valve center and use screen movement of the mouse/finger. Take the direction from the mouse to the valve, and the direction of movement. A cheap trick to check clock/counter-clockwise is taking the cross-product and checking whether z is negative (imagine making a finger-L: your pointer finger points to the valve center and your thumb points the way you are moving. Cross-product says which way your knuckles are aimed. In this case, towards or away from you.)

I think the same trick would work in world coords. Raycast onto a invisible plane around the wheel. But you'd have to compare the cross-product to the normal from the valve (dot product, which gives the cos of the angle? Pos or neg again.)

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 Michel Verheughe · Jan 01, 2013 at 09:02 PM 0
Share

Thank you for your answer, Owen. If I understand correctly, I must make four sectors, find which one the mouse is in by the difference of the position of the mouse and the center of the GameObject (the wheel of the valve, etc.) then see the direction of the mouse displacement to decide if it is a clockwise or counter-clockwise rotation.

I am new to Unity3D but I have several years with Flash ActionScript writing and I think I can do that. Another question, please:

I will have to do that UI for several objects of several scenes and projects. Would it then be correct for me to create a script named e.g. $$anonymous$$ain, where I gather all the routines doing that kind of operation? In a Flash environment, I would write something like:

var clockwise:Booleand = _root.checkRotation(Xcenter, Ycenter, Xmouse, Ymouse);

checkRotation then being a routine that can be called from any object and returns a boolean.

Would this be the proper way to work with Unity3D too?

Best regards, $$anonymous$$ichel

avatar image Owen-Reynolds · Jan 02, 2013 at 02:17 AM 0
Share

There wouldn't be any sectors or regions at all. No matter where the user clicks and moves, you compute 1) the direction line to the valve center, and 2) direction of movement. Then use math on those two lines. For example, you might also check if the motion is roughly at a right angle (not towards the valve,) using dot product.

Yes, Unity3D uses standard program$$anonymous$$g, including functions.

avatar image Michel Verheughe · Jan 02, 2013 at 08:44 AM 0
Share

Thank you Owen. I will now read about dot product! ;-)

Cheers, $$anonymous$$ichel

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

9 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

GetAxis difference between mobile and mouse 0 Answers

Rotation of Object on single axis in direction of the mouse position 0 Answers

Slerp/Rotational Problem Query 1 Answer

Object Look At Mouse 2 Answers

Smooth camera rotation on mobile and pc when GetMouseButton()? 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