- Home /
Analog clock hands rotation with onMouseDrag
Hello, I am making analog clock with movable hands. If I move hands separately they work well, but I want to make it possible when I move minute hand, hour hand should move too. Now, when hour hand is moved, it should skip 30 degrees, so it goes from one number to another. Also when minute hand moves, it skips 6 degrees, but I can't make hour hand skip 0.6 degrees. Here is the code:
void Update () {
if(Input.GetKeyUp(KeyCode.Escape))
{
Application.Quit();
}
if( Input.GetMouseButtonDown(0) )
{
objekatKliknut=RaycastFunct(Input.mousePosition);
if(objekatKliknut=="hand"){
angleHour = hand.transform.eulerAngles.z;
handRot = angleHour;
}
if(objekatKliknut=="hand1"){
angleHour = hand.transform.eulerAngles.z;
handRot = angleHour;
}
}
if(a1!=a2){
if(a1>a2){
handRot+=addAngle;
a2=a1;
}
else{
handRot-=addAngle;
a2=a1;
}
hand.transform.rotation = Quaternion.AngleAxis(handRot, Vector3.forward);
}
}
void OnMouseDrag(){
if(objekatKliknut=="hand1"){
mouseClickPos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
Vector3 dir = mouseClickPos - transform.position;
angle = Mathf.Atan2(dir.y,dir.x) * Mathf.Rad2Deg;
angle-=90;
if (angle < 0.0f) angle += 360.0f;
angle = Mathf.Round(angle/6.0f)*6.0f;
hand1.transform.rotation = Quaternion.AngleAxis(angle, Vector3.forward);
a1=angle;
}
}
Someone please help me, I need it badly. If my solution is not good, please suggest something else. Thank you.
Comment
Your answer
Follow this Question
Related Questions
Flip over an object (smooth transition) 3 Answers
Twin Stick Shooter controller 0 Answers
Controlling a rotating object 2 Answers
RotateAround Limitations 2 Answers
Distribute terrain in zones 3 Answers