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
1
Question by siaran · May 26, 2014 at 02:20 PM · rotationrelative rotation

rotate parent to aim child

Hello all,

I have a child object that I want it's forward transform to point at another gameobject. I need to rotate it by rotating its parent object. How do I calculate what the rotation of the parent object should be to get the child looking in the right direction?

Thank you for your time.

edit: also, note that the child object in question will most likely not be located at 0,0,0 relative to the parent object. Not sure if that is relevant tbh.

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 Sprawl · May 26, 2014 at 02:37 PM 0
Share

You would need to rotate the parent object the same rotation as you would need to rotate the child.

Was that the issue or did you want to know how to find that rotation ?

avatar image siaran · May 26, 2014 at 02:51 PM 0
Share

I want to know how to find that rotation :)

4 Replies

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

Answer by siaran · May 27, 2014 at 05:16 PM

I found this solution (through another post on this forum that I cannot find anymore):

     Quaternion relRot;
     
     void Start(){
             relRot = Quaternion.Inverse(transform.rotation) * child.transform.rotation;
             }
             
     void Update(){
             Vector3 targetDirection = target.transform.position - child.transform.position;
             Quaternion targetLook = Quaternion.LookRotation(targetDirection);
             Quaternion childTargetRot = Quaternion.RotateTowards(child.transform.rotation, targetLook, rotateSpeed*Time.deltaTime);
             Quaternion targetRot = Quaternion.Inverse(relRot) *childTargetRot;
             transform.rotation = Quaternion.RotateTowards(transform.rotation, targetRot, rotateSpeed * Time.deltaTime);
         
  }


which seems to do what I want. Quaternions aren't exactly my strong suit tho and calling Quaternion.RotateTowards twice seems a little weird. But this does what I want. Maybe I'll try your solution later but right now I am very happy to have it working and I don't want to touch it for a bit.

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 robochase · Jan 30, 2019 at 05:18 PM 0
Share

I've noticed that it gets a little jittery if the target gets too close, but otherwise it works rather well. Thanks!

avatar image
1

Answer by Maerig · May 27, 2014 at 02:01 AM

You can compute this rotation by combining the parent look rotation and the child inverted local rotation :

 public class CombinedRotations : MonoBehaviour {
 
     public  GameObject Parent, Child, Target;
     private Quaternion m_parentToChild;
     
     void Start() {
         Vector3 angles = Child.transform.localEulerAngles;
         m_parentToChild = Quaternion.Euler(-angles.x, -angles.y, -angles.z);
     }
 
     void Update() {
         Quaternion lookRotation = Quaternion.LookRotation(Target.transform.position - Parent.transform.position);
         Parent.transform.rotation = lookRotation * m_parentToChild;
     }
 }
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 Mccbbi · Jun 03, 2021 at 08:23 PM 0
Share

Thanks! This worked nice in my case. Children in my Parents had very small or zero local position offset. Other variants didnt work perfectly because their calculations were based on positions and directions. This solution is much better, its simple and universal.

avatar image
1

Answer by highpockets · Apr 02, 2019 at 09:35 AM

Ok, I found a pretty pure solution to this and the result works very well. I didn't want to have to add extra components (aim constraints or parent constraints) or extra game objects, etc.

My solution works as follows:

  • want child ("A") to aim at target (C") by rotating around parent ("B").

-since B is always going to be the same distance away from C, we can do some linear algebra and form a right angle triangle and use the distance from B to C as our hypotenuse and the point ("D") closest to B along the line that the forward vector of A produces will give us the point to form a 90 degree angle between D to B and the forward direction of A.

-Now the distance from B to C (hypotenuse) squared minus the distance from D to B (one side of the triangle) squared = the distance from D to the desired location of C relative to our setup ("E") squared. Once we have that we know that D + (C.transform.forward * distance to E) = E

-Now we take the cross product of D to E and D to C to get our xyz Quaternion rotation axis.

  • And we get our angle in radians as by getting the square root (dot product of D to E and D to E * dot product of D to C and D to C) + dot product of D to E and D to C. That is effectively our w rotation of quaternion.

-Now we build a new quaternion and normalize it new Quaternion(axis.x, axis.y, axis.z, angle).normalized and this is the reverse rotation of what is needed, thus we need to apply the inverse rotation to object B. B.transform.rotation = Quaternion.Inverse(ourNewQuaternion) * B.transform.rotation

That's it. Thought I would share in case someone else was looking to do the same.

Here is my code: (gun is my child, targetTrans is my target transform and spine is my parent)

  Vector3 rightAnglePoint = Vector3.Project(spine.position - gun.position, gun.forward * 5); //Get point to create 90 degree angle for right angle
 
             rightAnglePoint = gun.position + rightAnglePoint; //transform point to world space
 
             float sideC = Vector3.Distance(spine.position, targetTrans.position); //Get hypotenuse
 
             float sideA = Vector3.Distance(spine.position, rightAnglePoint); //Get sideA
 
             float sideB = Mathf.Sqrt((sideC * sideC) - (sideA * sideA)); //Get sideB. (C squared - A squared = B squared)
 
             Vector3 desiredRelTargetPos = rightAnglePoint + (gun.forward * sideB); //relative target point (if target were to rotate around spine to align with gun's forward direction)
 
             Vector3 spineToTargetDir = targetTrans.position - spine.position; //spine to target position direction
 
             Vector3 spineToRelTargetDir = desiredRelTargetPos - spine.position; //spine to desired target position relative to setup
 
             Vector3 rotAxis = Vector3.Cross(spineToTargetDir, spineToRelTargetDir); //get rotation axis
 
             float rotAngle = Mathf.Sqrt(Vector3.Dot(spineToTargetDir, spineToTargetDir) * Vector3.Dot(spineToRelTargetDir, spineToRelTargetDir)) + Vector3.Dot(spineToTargetDir, spineToRelTargetDir); //Get rotation angle
 
             Quaternion inverseRot = new Quaternion(rotAxis.x, rotAxis.y, rotAxis.z, rotAngle).normalized; //Construct new Quaternion
 
             spine.rotation = Quaternion.Inverse(inverseRot) * spine.rotation; //Apply rotation



Cheers

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 hawaiian_lasagne · Jan 18, 2021 at 09:06 PM 0
Share

Wow man I don't know what this is doing but you've saved my a**. Thank you very much!

avatar image
0

Answer by robochase · Jan 31, 2019 at 04:11 AM

Since this question was posed, Unity has introduced handful of classes that make this much easier. The class you're looking for is the ParentConstraint class.

In a nutshell, you want to put this component onto the parent, and add the child as a source (make sure to click "Activate" to seal the deal). This basically makes it so that the child is the new parent.

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 highpockets · Mar 28, 2019 at 09:21 PM 0
Share

Hmm, are you suggesting to do this and then rotate around the real parent?? I think the question was to make the child point at a target but needing to rotate around the parent position to get there. If the child becomes the parent, this would be counter productive, or am I missing something? Sorry to dig up such an old post, but I want to do this, but can't find an elegant way to do so.

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

How to Rotate Relative To World 1 Answer

Add rotation from an angle 1 Answer

transform.rotation and localRotation automatically reset after rotating. 1 Answer

rotate object on transform.right 3 Answers

Flip over an object (smooth transition) 3 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