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
2
Question by firesharkstudios · May 17, 2012 at 06:36 PM · rotationquaternion

Parent Rotation to Get Target Child Rotation

Assuming that I have a parent transform that is a parent to a child transform (not necessarily a direct parent though, could be several levels between parent and child), I need a function to calculate the proper world rotation of the parent transform in order to achieve a specific target world rotation of the child transform.

So, this would look something like...

 // Should return the correct value for parent.rotation such 
 // that child.rotation equals targetChildRotation
 // (assuming that parent is a direct or indirect parent of child)
 public static Quaternion CalculateParentRotation(Transform parent, Transform child, Quaternion targetChildRotation) {
     // What goes here?
 }

Probably a simple problem but the answer is escaping me. What does this function look like?

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 firesharkstudios · May 17, 2012 at 08:30 PM 0
Share

So, if C represents the world rotation of the child, P represents the world rotation of the parent, and R represents the relative rotation between the C and P then I believe this can be expressed as...

R = Quaternion.Inverse(C) * P

Or alternatively...

C = R * P

However, I basically need to solve for P given that I know R and C. Hmmmm.

avatar image hathol · May 17, 2012 at 11:15 PM 0
Share

P *= Inverse(C)*TargetChildRot ?

2 Replies

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

Answer by aldonaletto · May 18, 2012 at 12:23 AM

You must find the children rotation relative to the root object at Start: "divide" the children rotation by the root rotation. If you find the target rotation somehow (with LookRotation or FromToRotation, for instance), you can just "divide" it by the relative rotation:

Quaternion relRot; public Transform rootObj;

void Start(){ relRot = Quaternion.Inverse(rootObj.rotation) * transform.rotation; }

void AlignParent(Quaternion targetChildRot){ rootObj.rotation = Quaternion.Inverse(relRot) targetChildRot; } Maybe the "division" order is reversed (should it be Inverse rotation?) - I can't test this right now, but will do it soon and edit the question if there's any error.

EDITED: In fact, the order was reversed - it's fixed now. Alternatively, it's possible to avoid one Inverse operation if relRot is calculated in the reverse order, because this would be the result of rootObj.rotation/transform.rotation:

Quaternion relRot; public Transform rootObj;

void Start(){ relRot = transform.rotation * Quaternion.Inverse(rootObj.rotation); }

void AlignParent(Quaternion targetChildRot){ rootObj.rotation = relRot * targetChildRot; }

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 firesharkstudios · May 18, 2012 at 12:25 AM

Turns out to be just basic matrix math -- here is the solution...

C = P * R

where C is child, P is parent, and R is relative rotation between parent and child then you can solve for P or R as follows...

P = C * Quaternion.Inverse(R)

R = Quaternion.Inverse(P) * C

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 Dante_CyberdeckGames · Apr 12, 2017 at 06:26 PM 0
Share

This solution is correct for any situation where parent and child are facing any arbitrary direction.

For also calculating the child to target rotation, here is my full working code:

 // Current and target child rotations
 Vector3 newChildForward = lookTarget.transform.position - child.transform.position;
 newChildForward.Normalize();
 Quaternion C1 = child.transform.rotation;
 Quaternion C2 = Quaternion.LookRotation(newChildForward, child.transform.up);
 
 // Relative rotation
 Quaternion R = Quaternion.Inverse(parent.transform.rotation) * C1;
 
 // Parent rotation to match target child rotation
 Quaternion P = C2 * Quaternion.Inverse(R);
 parent.transform.rotation = P;
 

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

7 People are following this question.

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

Related Questions

Is there no way to get reliable angles from a rigidbody? 2 Answers

Quaternion Rotation On Specific Axis Issue 0 Answers

Look at like rotating around y axis 1 Answer

ConfigurableJoint - angular positions (rotation) problem 1 Answer

Make Quaternion affected by float 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