Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 Clonkex · Jun 14, 2014 at 08:19 AM · rotationquaternioneuleraddtorque

Use RigidBody.AddTorque with Quaternions or Forward/Up/Right vectors?

I need to use the AddTorque function to "push" a RigidBody towards a specific angle. Last time I tried to do this I spent several days, failed, and gave up. I've come back to this problem because I really want to make this game.

What I am trying to do is this, copied from my less-specific Stack Overflow question:

I'm trying to play an animation on a humanoid bone structure and, using torque (rotational force), push the physics ragdoll into approximately the same pose as the bone structure.

So far I've been assuming that I need to convert my Quaternion rotation to a Euler angle in order to use it with AddTorque, but some helpful guy on Stack Overflow pointed out that the function doesn't actually work directly with Euler angles. This got me thinking.

My question is this: Is it possible to use RigidBody.AddTorque with either Quaternion rotations or the Forward/Up/Right vectors of the transform to "push" a rigidbody toward a specific angle (this specific angle being stored as a Quaternion)? The obvious way is to get the Euler angles out of the Quat, but this is unreliable and the physics tends to spaz severely.

I'd appreciate any help on this :)

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
0
Best Answer

Answer by Clonkex · Jun 15, 2014 at 01:08 AM

Some awesome guy from Stack Overflow solved my problem for me :D I posted an answer, because he worked it out entirely in the comments:

http://stackoverflow.com/a/24225689/2288578

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

Answer by HiddenMonk · May 28, 2015 at 07:03 AM

Using the answer from the stackoverflow post above, I was able to make this.

 using UnityEngine;
 
 public static partial class ExtRigidbody
 {
     public static void MoveRotationTorque(this Rigidbody rigidbody, Quaternion targetRotation)
     {
         rigidbody.maxAngularVelocity = 1000;
 
         Quaternion rotation = targetRotation * Quaternion.Inverse(rigidbody.rotation);
         rigidbody.AddTorque(rotation.x / Time.fixedDeltaTime, rotation.y / Time.fixedDeltaTime, rotation.z / Time.fixedDeltaTime, ForceMode.VelocityChange);
         rigidbody.angularVelocity = Vector3.zero;
     }
 }
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 Clonkex · May 28, 2015 at 09:18 AM 0
Share

I haven't worked on this project for some considerable time now so I can't tell off the top of my head if this code would work, but assu$$anonymous$$g it does this could be very helpful to some people. Thanks for posting :)

avatar image Derakon · Aug 13, 2019 at 05:46 AM 2
Share

This mostly works, but because it only uses 3 of the 4 components of the quaternion, it goes awry in some edge cases. Try multiplying the torque vector by rotation.w, e.g.

 var torque = new Vector3(rotation.x, rotation.y, rotation.z) * rotation.w * Time.fixedDeltaTime;
avatar image CometGoat Derakon · Apr 25, 2020 at 11:09 PM 0
Share

Oh my god THANK YOU! Now my torque doesn't try to take a shortcut!

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

25 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

Related Questions

Rotation math question: car movement on a cylinder 0 Answers

Turret on Tank rotating fine until I'm not flat on ground 1 Answer

Why is Quaternion.Eular only rotating in one direction? [Answered] 1 Answer

Trouble Rotating Full-Circle 0 Answers

trouble with Quaternion rotation in 360 axis degree 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