Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 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
3
Question by yakri · Sep 17, 2017 at 07:05 PM · quaterniontransform.rotation

Rotating a parent object to achieve a specific child rotation.

Alright so I have kind of a weird problem. I need to rotate a parent object in order to orient a child object to a specific direction.

I'm working in VR which means my child object is tracked and I can't fuck with its rotation in any way, I can only rotate the parent object. Now I need to align this child object to some variable direction at runtime by changing the parent orientation.

This would be pretty easy if I could unparent the child or rotate the child but neither are options. I also have to use a quaternion method of some kind to get a smooth rotation.

So far I've had zero luck managing to make this happen though so any help would be appreciated. I imagine this ought to be fairly easy to do with the right knowledge of quaternion math but I haven't had any luck working it out over the last day.

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
6

Answer by yakri · Sep 18, 2017 at 12:08 PM

Ok, I'm pretty sure I've solved this for those questing for quaternion rotation solutions in the future.

If you have a look rotation for the direction and alignment you want your child object to have (or any equivalent quaternion) you take the inverse of the child object's rotation times the look rotation (quaternion subtraction) like so:

 lookRotationVar = lookRotationVar * Quaternion.Inverse(childObject.transform.rotation);

then you need to add that look rotation to the parent object's rotation

 lookRotationVar = lookRotationVar * parentObject.transform.rotation;

Now lookRotationVar will be the rotation you want to rotate your parent object towards in order to correctly orient your child object.

At the very least, this has worked through all my testing so far. In particular though I've only tested this working with steamVR and having the camera be the child object.

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 e5me · Aug 20, 2019 at 08:23 AM 0
Share

You have ended my quest, thank you

avatar image byBrickDaniel · Oct 29, 2021 at 07:44 AM 0
Share

This saved me! Thank you so much <3

avatar image thangdd · Nov 29, 2021 at 10:50 PM 0
Share

could you please help me to write an example code for this @@ i'm stucking with this :(

avatar image
0

Answer by PerfectlyNormalBeast · Aug 25, 2020 at 11:54 AM

I had a similar yet different scenario that this helped me with - figured I'd share:

I'm working on an inverse kinematic that allows the arms to be positioned wherever then it sets the joint's position/rotation to be relative to the arm positions

(the joint is represented as Plane)

The arms are represented by vector1 and vector2 (calculated elsewhere and passed to this code)

Since the arm positions are fuzzy relative to the parent joint, I'm taking the bisecting line and cross product as my two vectors that will control the rotation

---------- Initialize

 public GameObject Plane;
 private Quaternion _delta;
 
 Vector3 bisect = Math3D.GetAverage(new[] { vector1, vector2 }).normalized;
 Vector3 orth = Vector3.Cross(bisect, vector1).normalized;
 
 Quaternion bisectRot = Quaternion.LookRotation(bisect, orth);
 
 _delta = bisectRot * Quaternion.Inverse(Plane.transform.rotation);
 

---------- Update

 Vector3 bisect = Math3D.GetAverage(new[] { vector1, vector2 }).normalized;
 Vector3 orth = Vector3.Cross(bisect, vector1).normalized;
 
 Plane.transform.rotation = Quaternion.LookRotation(bisect, orth) * _delta;

So in these pictures, the orange lines are the arms, the purple are the bisector and orthogonal

(I placed the arms on the plane to help debugging and visualization, but this works for any initial positions)

alt text alt text


initial.png (48.9 kB)
after-dragging.png (74.1 kB)
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

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

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

Rotate children (Cards) on a panel (maybe using lerp, or another method?) 1 Answer

How do I clamp the Z-Axis Rotation for this code? 1 Answer

Store object's starting position and rotation and have it return there. 2 Answers

Calculate sum over all movements and rotations of object 1 Answer

Quaternion.LookRotation() doesn't look down 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