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
2
Question by Maurdekye · May 29, 2016 at 02:36 AM · rigidbodyquaterniontorquealignment

Apply torque to align rigidbody to a specific angle in 3D

I have a rigidbody ObjectToAttract and an empty gameobject with a script attached to it. I would like to apply torque to the rigidbody in order to align its rotation with the empty gameobject. Here's the basic code for the script;

 Quaternion AngDifference = Quaternion.Inverse(ObjectToAttract.transform.rotation) * (transform.rotation);
 Vector3 AngDiffMovement = RectifyAngleDifference(AngDifference.eulerAngles);
 ObjectToAttract.AddTorque(AngDiffMovement - ObjectToAttract.angularVelocity, ForceMode.VelocityChange);

And here's the RectifyAngleDifference function;

 private Vector3 RectifyAngleDifference(Vector3 angdiff)
 {
     if (angdiff.x > 180) angdiff.x -= 360;
     if (angdiff.y > 180) angdiff.y -= 360;
     if (angdiff.z > 180) angdiff.z -= 360;
     return angdiff;
 }

For some angles of the empty object the script works perfectly fine. But for others the rigidbody wobbles around in seemingly random directions. How can I fix that?

I have a separate revision of the above code;

 Quaternion AngDifference = Quaternion.FromToRotation(ObjectToAttract.transform.up, transform.up);
  Vector3 AngDiffMovement = RectifyAngleDifference(AngDifference.eulerAngles);
  ObjectToAttract.AddTorque(AngDiffMovement - ObjectToAttract.angularVelocity, ForceMode.VelocityChange);

Which is significantly more stable, however it ignores an entire axis of rotation.

Comment
Add comment · Show 4
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 JedBeryll · May 29, 2016 at 05:55 AM 0
Share
 if (angdiff.x > 180) angdiff.x -= 360;
 if (angdiff.y > 180) angdiff.y -= 360;
 if (angdiff.z > 180) angdiff.z -= 360;

I don't know about this part - shouldn't you be subtracting 180?

avatar image Maurdekye JedBeryll · May 29, 2016 at 03:58 PM 0
Share

Did you make the change yourself, test it, and deter$$anonymous$$e that it worked better with that change? Or did you just guess?

avatar image Maurdekye JedBeryll · May 29, 2016 at 04:03 PM 0
Share

And no, that didn't work when I tested it.

avatar image JedBeryll Maurdekye · May 29, 2016 at 05:40 PM 0
Share

Was just a guess because i didn't see anything else.

2 Replies

· Add your reply
  • Sort: 
avatar image
4

Answer by Maurdekye · May 30, 2016 at 03:46 AM

I solved my problem by using the second algorithm and applying a secondary corrective vector in addition to the main angular vector. Here is what the full code looks like;

 Quaternion AngleDifference = Quaternion.FromToRotation(ObjectToAttract.transform.up, transform.up);
 
 float AngleToCorrect = Quaternion.Angle(transform.rotation, ObjectToAttract.transform.rotation);
 Vector3 Perpendicular = Vector3.Cross(transform.up, transform.forward);
 if (Vector3.Dot(ObjectToAttract.transform.forward, Perpendicular) < 0)
     AngleToCorrect *= -1;
 Quaternion Correction = Quaternion.AngleAxis(AngleToCorrect, transform.up);
 
 Vector3 MainRotation = RectifyAngleDifference((AngleDifference).eulerAngles);
 Vector3 CorrectiveRotation = RectifyAngleDifference((Correction).eulerAngles);
 ObjectToAttract.AddTorque((MainRotation - CorrectiveRotation/2) - ObjectToAttract.angularVelocity, ForceMode.VelocityChange);
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 SamFernGamer4k · Apr 26, 2020 at 11:56 PM 0
Share

Than you very much. That's exactly what I was searching for a long time.

avatar image motif_nostalgia · May 26, 2020 at 10:48 AM 0
Share

dude, this saved me so much. hats off to you for being the only one who apparently understands rotation

avatar image
0

Answer by CMHatch · Sep 29, 2020 at 11:07 PM

I found this solution to be super elegant and much more easy to wrap my head around when using a Vector 3 to define the desired rotation.

 var rot = Quaternion.FromToRotation(transform.up, Vector3.up);
  rb.AddTorque(new Vector3(rot.x, rot.y, rot.z)*uprightTorque);


Source unity forum post for the code above

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 SamFernGamer4k · Sep 30, 2020 at 01:46 PM 0
Share

rot.eulerAngles should work better than removing the w component from the quaternion. It should cause problems because quaternions have a logic different from Euler rotations. Also, it's better to use "rot" as a target velocity instead of torque, because it would cause wobbling when the force is too big. Anyway, I'm going to try it out as soon as possible.

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

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

Aligning 2 Quaternions Using Torque 1 Answer

Figuring out the correct amount of torque to apply? 2 Answers

Choppy movement when rotating 2 Answers

Rotate player (rigidbody) towards his movement 2 Answers

Rigidbody insists on adding torque around child object's origin [C#] 0 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