Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 JamKek · Oct 23, 2016 at 09:08 AM · rotationtransforminput

Drag object in a circle path

Hello, I'm trying to rotate an object with mouse/touch input, like thisalt text

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.

photo.png (112.6 kB)
Comment
Add comment · Show 3
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 doublemax · Oct 23, 2016 at 09:11 AM 1
Share

$$anonymous$$aybe this helps:

http://answers.unity3d.com/questions/1256608/move-an-object-in-a-circular-path-according-to-tou.html

avatar image JamKek doublemax · Oct 23, 2016 at 03:59 PM 0
Share

That's interesting, maybe I could rotate the wheel based on the position of the handle...

avatar image JamKek doublemax · Oct 23, 2016 at 04:35 PM 0
Share

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!!!!

1 Reply

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

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 !!!!!

Comment
Add comment · 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

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

4 People are following this question.

avatar image avatar image avatar image avatar image

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


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