- Home /
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
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.
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)
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
@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 :)
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
Your answer
Follow this Question
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