- Home /
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
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.)
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
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.
Thank you Owen. I will now read about dot product! ;-)
Cheers, $$anonymous$$ichel