- Home /
 
Drag object in a circle path
Hello, I'm trying to rotate an object with mouse/touch input, like this
I need to hold the green circle, and drag it around 360 degrees, after which I output something. The code I have now just mimics it.
  //Image Wheel = the white wheel, defined in inspector
  //Image Handle = the green handle which,while user touches it,sets isRotating to true
  //float rotAngle = current angle, 0 at start
  
  public void Update(){
         if(isRotating){
             Wheel.transform.rotation = Quaternion.AngleAxis(rotAngle, Vector3.forward);
             rotAngle += 9f;
         }
         if(rotAngle > 360){
             rotAngle = 0;
             Debug.Log("Full circle! / Points++");
         }
     }
     
     WheelHandleDown(){
         isRotating = true;
     }
     WheelHandleUp(){
         isRotating = false;
     }
 
               It rotates the wheel while the player is holding the green handle. This creates an illusion that the player is actually dragging the handle and rotating the wheel.
But the problem I have is that if I try to rotate the wheel very fast, the mouse leaves the handle and it does not rotate. If I increase the rotAngle which is increased every update while the player is "spinning" the wheel, the action looks very choppy. If I decrease it, the action looks smooth but the player can't rotate the wheel as fast.
Is there a way to move the green handle to the mouse/touch position,without leaving the circle path?
I've tried various answers to similar questions, which involved sin,cos,atan functions which I still don't understand.
$$anonymous$$aybe this helps:
http://answers.unity3d.com/questions/1256608/move-an-object-in-a-circular-path-according-to-tou.html
That's interesting, maybe I could rotate the wheel based on the position of the handle...
Looks like I did it!
 Vector3 diff = Input.mousePosition - Wheel.transform.position;
 Vector3 newPos = Wheel.transform.position + diff.normalized * WheelRadius;
 Vector2 dir = newPos - Wheel.transform.position;
 float angle = $$anonymous$$athf.Atan2(dir.x, dir.y) * $$anonymous$$ath.Rad2Deg;
 Wheel.transform.rotation = Quaternion.AngleAxis(-angle,Vector3.forward);
 
                   I'm sure this code needs fixing but it works, and I have no idea why :D Thank you so much!!!!
Answer by JamKek · Oct 23, 2016 at 08:23 PM
Thanks to @doublemax it's working! Here's the code if anyone ever needs it in the future
 public Image Wheel; //the thing you're trying to rotate
 Vector2 dir;
 float dist;
 float check;
 
     void Update(){
         //is rotating is set true if mouse is down on the handle
         if (isRotating) {
             //Vector from center to mouse pos
             dir  =    (Input.mousePosition - Wheel.transform.position); 
             //Distance between mouse and the center
             dist =    Mathf.Sqrt (dir.x * dir.x + dir.y * dir.y); 
             //if mouse is not outside nor too inside the wheel
             if (dist < 350 && dist > 150) {
                 angle =    Mathf.Atan2(dir.x, dir.y) * Mathf.Rad2Deg; //alien technology
                 angle =    (angle > 0) ? angle : angle + 360; //0 to 360 instead of -180 to 180
     
                 //this if blocks going back or jumping too far
                 if ( ( angle<check && check-angle<90) || angle>350 ) {
                     check = angle;
                     Wheel.transform.rotation = Quaternion.AngleAxis (angle, Vector3.back);
                     //Vector3.back for counter clockwise, Vector3.forward for clockwise..I think
                 }
             }
         }
         //to confirm if it has passed full circle
         if(angle > 160 && angle < 200){
             checkPoint = true;
         }
     
         if(angle > 350 && checkPoint){
             checkPoint = false;
             Debug.log("SCORE++");
         }
     }
     
     public void BeginDrag(){
         isRotating = true;
     }
     public void EndDrag(){
         isRotating = false;
     }
 
               If anyone wants to see it working, check out my disaster at Github , no judging please :D
Again, huge thanks to @doublemax !!!!!
Your answer
 
             Follow this Question
Related Questions
Rotation-Trouble 4 Answers
Key input to rotate object 2 Answers
Transform.Rotate not stopping? 1 Answer
Scale gameObject 1 Answer
How to prevent Z axis rotation ? 1 Answer