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 nik_2522 · Apr 17, 2016 at 12:15 PM · vector3directionquaternionsdragging

Restrict Direction of rotation in Realtime.

I have a hand of clock which the player once starts rotating (Dragging) CCW until he completes one full rotation. ( Without lifting drag )

I am trying to lock the rotation to only CCW direction while/once the player starts rotating. I got help from the following links : Detect Direction by @BobBobson108

Here is gif of what is actually happening: Demo

Below is what I actually want: alt text

Here is my code :

 void OnMouseDrag()
     {
         //rotation
         Vector3 mousePos = Input.mousePosition;
         mousePos.z = 5.23f;        
 
         Vector3 objectPos = Camera.main.WorldToScreenPoint(transform.position);        
         mousePos.x = mousePos.x - objectPos.x;
         mousePos.y = mousePos.y - objectPos.y;
         angle = Mathf.Atan2(mousePos.y, mousePos.x) * Mathf.Rad2Deg;
         transform.rotation = Quaternion.Euler(new Vector3(0, 0, angle - 90f));
 
        

         hand_vector = transform.up;
         cross_product = Vector3.Cross(ref_vector, hand_vector);
         dot_product = Vector3.Dot(cross_product, transform.forward*-1);
         //Debug.Log("Hand Vector: " + hand_vector);
         //Debug.Log("Ref Vector: " + ref_vector);
         Debug.Log(cross_product);
         Debug.Log(dot_product);
     }

I tried to debug the values of the cross product, but the direction of resultant vector seems to be same even when when the player starts backward rotation. alt text

Also the cross product vector changes direction only when the player starts rotation in CW direction from the default position i.e. 12 'o clock.

I have very less experience of working with Quaternions and rotations. Any help will be highly helpful. Thanks !!!

diagram.png (27.3 kB)
capture.png (28.9 kB)
Comment
Add comment · Show 2
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 JoeStrout · Apr 20, 2016 at 01:54 PM 1
Share

You don't need quaternions, cross-products, or Vector3's here. Just use $$anonymous$$ath.Atan2 to find the angle, as shown here.

avatar image nik_2522 JoeStrout · Apr 24, 2016 at 06:42 AM 0
Share

Thanks for the answer. but I figured out. Infact a couple of solutions which I will post here. It was quite easy though. I am still new to Unity, hence I missed it.

1 Reply

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

Answer by nik_2522 · Apr 24, 2016 at 07:35 AM

Hey Guys, I manged to figure out the issue blocking me. Hope this post helps someone in future who is new to Unity rotations, vectors and Maths.

void OnMouseDrag() {

     transform.Rotate(new Vector3(0,0, Mathf.Sqrt(Input.GetAxis("Mouse X") * Input.GetAxis("Mouse X") + Input.GetAxis("Mouse Y") * Input.GetAxis("Mouse Y"))));  
     
  /*      
     Vector3 mousePos = Input.mousePosition;
     mousePos.z = 5.23f;

     Vector3 objectPos = Camera.main.WorldToScreenPoint(transform.position);
     mousePos.x = mousePos.x - objectPos.x;
     mousePos.y = mousePos.y - objectPos.y;

     print("Angle is :");
     print(Mathf.Atan2(mousePos.y, mousePos.x) * Mathf.Rad2Deg - 90f);

     if (counter_clockwise)
     {
         if (Mathf.Atan2(mousePos.y, mousePos.x) * Mathf.Rad2Deg - 90f > 0)
         {
             angle = Mathf.Max(angle, Mathf.Atan2(mousePos.y, mousePos.x) * Mathf.Rad2Deg - 90);
             transform.rotation = Quaternion.Euler(new Vector3(0, 0, angle));
         }


         if (Mathf.Atan2(mousePos.y, mousePos.x) * Mathf.Rad2Deg - 90f < 0)
         {
      angle = Mathf.Max(angle, 360 + (Mathf.Atan2(mousePos.y, mousePos.x) * Mathf.Rad2Deg - 90));
             transform.rotation = Quaternion.Euler(new Vector3(0, 0, angle));
         }
     }            
            
 }    



Comment
Add comment · Show 1 · 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 nik_2522 · Apr 24, 2016 at 06:47 AM 0
Share

Here, I am first checking whether the player is first rotating in clockwise direction or anticlockwise direction via this code :

 void On$$anonymous$$ouseDown()
     {
         Vector3 mouseDragStartPos = Input.mousePosition;
         
         print("$$anonymous$$ouse pos:  " + mouseDragStartPos);
         print(" ref_vector" + ref_vector);
 
         if (mouseDragStartPos.x < ref_vector.x)
         {
             clockwise = false;
             counter_clockwise = true;
             print("Left !!!");
         }
 
         if (mouseDragStartPos.x >= ref_vector.x)
         {
             clockwise = true;
             counter_clockwise = false;
             print("Right !!!");
         }
     }

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Transform angle to grid position 2 Answers

Make a moving object continue it's movement from the same direction , even it reaches target 1 Answer

"Fake" Vector3.forward? 3 Answers

How to rotate an object to face the direction it's going? 1 Answer

Fit line through cluster of points 2 Answers


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