Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 Aladine · May 27, 2014 at 08:55 PM · 2drotationwheelspin

Spin a 2D object with mouse drag

Hello everyone,

I am trying to have a control like in the game in this youtube video and so far i am having issues with transform.rotate, i tried to look on similar questions and so far this is what i've got :

 using UnityEngine;
 using System.Collections;
 
 public class DragRotateSlowDown : MonoBehaviour
 {
     
         private float rotationSpeed = 10.0F;
         private float lerpSpeed = 1.0F;
         private Vector3 theSpeed;
         private Vector3 avgSpeed;
         private bool isDragging = false;
 
         void OnMouseDown ()
         {
         
                 isDragging = true;
         }
     
         void Update ()
         {
         
                 if (Input.GetMouseButton (0) && isDragging) {
                         theSpeed = new Vector3 (-Input.GetAxis ("Mouse X"), Input.GetAxis ("Mouse Y"), 0.0F);
                         avgSpeed = Vector3.Lerp (avgSpeed, theSpeed, Time.deltaTime * 5);
                 } else {
                         if (isDragging) {
                                 theSpeed = avgSpeed;
                                 isDragging = false;
                         }
                         float i = Time.deltaTime * lerpSpeed;
                         theSpeed = Vector3.Lerp (theSpeed, Vector3.zero, i);
                 }
         
                 transform.Rotate (Camera.main.transform.forward * theSpeed.x * rotationSpeed, Space.World);
                 transform.Rotate (Camera.main.transform.forward * theSpeed.y * rotationSpeed, Space.World);
 
         }
 }

but there is a lot of problems with this solution, mainly, the object doesn't continue to rotate in a certain moment, it "stop" following the mouse and instead it rotate in the opposite direction.

Any help will be heavily appreciate it, thank you all and have a great day.

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

2 Replies

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

Answer by Aladine · May 29, 2014 at 12:39 AM

thnx itsa for your help, here is the final code with the wanted effect :-)

 using UnityEngine;
 using System.Collections;
 
 public class NewBehaviourScript : MonoBehaviour
 {
         public float deltaRotation;
         public float deltaLimit;
         public float deltaReduce ;
         float previousRotation;
         float currentRotation;
 
         void Start ()
         {
                 Screen.showCursor = false;
         }
     
         void Update ()
         {
                 if (Input.GetMouseButtonDown (0)) {
                         deltaRotation = 0f;
                         previousRotation = angleBetweenPoints (transform.position, Camera.main.ScreenToWorldPoint (Input.mousePosition));
                 } else if (Input.GetMouseButton (0)) {
                         currentRotation = angleBetweenPoints (transform.position, Camera.main.ScreenToWorldPoint (Input.mousePosition));
                         deltaRotation = Mathf.DeltaAngle (currentRotation, previousRotation);
                         if (Mathf.Abs (deltaRotation) > deltaLimit) {
                                 deltaRotation = deltaLimit * Mathf.Sign (deltaRotation);
                         }
                         previousRotation = currentRotation;
                         transform.Rotate (Vector3.back * Time.deltaTime, deltaRotation);
                 } else {
                         transform.Rotate (Vector3.back * Time.deltaTime, deltaRotation);
                         deltaRotation = Mathf.Lerp (deltaRotation, 0, deltaReduce * Time.deltaTime);
                 }
                 
         }
     
         float angleBetweenPoints (Vector2 position1, Vector2 position2)
         {
                 Vector2 fromLine = position2 - position1;
                 Vector2 toLine = new Vector2 (1, 0);
         
                 float angle = Vector2.Angle (fromLine, toLine);
                 
                 Vector3 cross = Vector3.Cross (fromLine, toLine);
                 
                 // did we wrap around?
                 if (cross.z > 0) {
                         angle = 360f - angle;
                 }
         
                 return angle;
         }
 
         
 }
Comment
Add comment · Show 2 · 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
avatar image goutham12 · Mar 18, 2018 at 09:40 AM 0
Share

works like charm....

avatar image arashsh · Feb 28, 2020 at 11:08 PM 0
Share

when i change deltarotation value in inspector work great but working with mouse or touch why?!!

avatar image
3

Answer by itsa · May 27, 2014 at 09:34 PM

 using UnityEngine;
 using System.Collections;
 
 public class NewBehaviourScript : MonoBehaviour 
 {
     public Transform cube;
 
     float deltaRotation;
     float previousRotation;
     float currentRotation;
 
     void Start () 
     {
     
     }
     
     void Update () 
     {
         if (Input.GetMouseButtonDown (0))
         {
             deltaRotation = 0f;
             previousRotation = angleBetweenPoints( cube.position, Camera.main.ScreenToWorldPoint(Input.mousePosition));
         }
         else if(Input.GetMouseButton(0))
         {
             currentRotation = angleBetweenPoints( cube.position, Camera.main.ScreenToWorldPoint(Input.mousePosition) );
             deltaRotation = Mathf.DeltaAngle( currentRotation, previousRotation );
             previousRotation = currentRotation;
             cube.Rotate( Vector3.back, deltaRotation );
         }
 
     }
 
     float angleBetweenPoints( Vector2 position1, Vector2 position2 )
     {
         var fromLine = position2 - position1;
         var toLine = new Vector2( 1, 0 );
         
         var angle = Vector2.Angle( fromLine, toLine );
         var cross = Vector3.Cross( fromLine, toLine );
         
         // did we wrap around?
         if( cross.z > 0 )
             angle = 360f - angle;
         
         return angle;
     }
 }

adapted from TouchKit - TKOneFingerRotationRecognizer

Comment
Add comment · Show 3 · 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
avatar image itsa · May 28, 2014 at 09:01 PM 0
Share

Did it help?

avatar image Aladine · May 28, 2014 at 10:45 PM 0
Share

(sorry for the delay) yes it helped a lot :-) i will try to figure out myself how to add a more "realistic" feeling to it (keep rotating a little bit based on how fast the manual rotation was)

avatar image itsa · May 28, 2014 at 10:50 PM 0
Share

glad i could help :)

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

23 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How Rotate Object To Resemble Spinning? 1 Answer

2D wheel spin 2 Answers

2D LookAt not working as intended 1 Answer

Pysics not working as expected 1 Answer

how to rotate 2d obj on android 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