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 Chom1czek · Dec 01, 2015 at 01:37 PM · rotationrigidbodyrigidbody.velocityangularvelocity

Rigidbody angularvelocity shortest way

I'm using rigidbody velocity to move around and I use mouse clicks to check which direction should my player character go

I don't know how to check when my rigidbody should make turn left or right, what should the if statement look like to choose the shortest way because if I use only right turn and then I click somewhere where left turn could do just 10 degrees to the left and it's done it still makes right turn and does 170 degrees to the right to meet the condition to stop.

             if(Input.GetMouseButtonDown(0))
             {
                 Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
                 RaycastHit hit;
                 if (Physics.Raycast(ray, out hit, 1000f))
                 {
                     clickPos = hit.point;
                     clickPos.y = 0f;
                 }
 
                 //playerRigidbody.velocity = Vector3.zero;
                 playerToMouse = clickPos - transform.position;
                 playerToMouse.y = 0f;
                 newRotation = Quaternion.LookRotation(playerToMouse);
                 Debug.Log(Quaternion.Angle(newRotation, playerRigidbody.rotation));
                 //playerRigidbody.MoveRotation(newRotation);

                 if( something >= something) <--- what should I put here?
                 {
                 playerRigidbody.angularVelocity = Vector3.down * ANGULARRIGID;
                 }
                  else 
                  {
                  playerRigidbody.angularVelocity = Vector3.up* ANGULARRIGID;
                  }
 
             }



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

1 Reply

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

Answer by Bunny83 · Dec 01, 2015 at 01:45 PM

Simply this:

 if (Vector3.Dot(transform.right, playerToMouse) > 0)

You might need to swap the greater-than with a lower-than.

The dot product returns 0 when the two vectors are perpendicular to each other. If they point in the same direction it returns a positive value if it points in the opposite direction a negative value. So if it's positive you should turn right.

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 Chom1czek · Dec 01, 2015 at 01:55 PM 0
Share

It works perfectly, thank you very much :) I were using the Quaternion.Angle before but it just doesn't fit that role :) A lot work ahead of me if I didn't make it work but I'm glad that you helped me fast. Cheers!

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

34 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Stop velocity on rotation change 2 Answers

Gravity on a rotating platform 1 Answer

How to rotate an object to a specific angle with angularVelocity? 0 Answers

Calculate Angular Velocity to Match Remote Position 0 Answers

How to stop a physics object slowly 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