- Home /
Rotating/Aligning parents relative to their children
Hello all. I have having some trouble alinging two parent objects, according to their child objects.
In this picture, the blue squares are the parents, while the red dots are the children I am trying to align by. The yellow line is the rotation of the children.
Positioning them at the same world spot is easy,
parentPosition =otherParentsChildWorldPos- thisParentsChildsLocalPosition;
Now rotating them so that the rotation are the inverse of each other is proving to be more difficult. I have tried ways such as
var desiredRot = otherChild.transform.eulerAngles - thisChild.transform.eulerAngles;
var offsetBetweenParentAndItsChild= thisChild.transform.localEulerAngles;
parent.transform.rotation = Quaternion.Euler(-otherChild.transform.eulerAngles + offsetBetweenParentAndItsChild);
But it provides wonky results when you rotate the parent from a rotation other than 0,0,0.
Any ideas?
A possible theory are somehow treating the child position as the origin on the parent (even when it's not), and rotating from there?
Answer by ahstein · Jun 26, 2018 at 11:14 PM
Shouldn't the local Euler angles for the red dots always be (0,0,0)? Are you rotating the red dots independently of their parents?
If the red dots are in a fixed position relative to the origin of the parents, then you can use that as a "heading" for the object and use Quaternion.SetFromToRotation to find the correct rotation (multiply the target heading by -1 to rotate it by 180 deg).
Something like...
transform.rotation *= Quaternion.SetFromToRotation(child.transform.localPosition,
-1 * otherChild.transform.localPosition);
Your answer
Follow this Question
Related Questions
Get Children of Child and Deactivate/Activate on Demand 1 Answer
Problem with prefab parent and children objects that need to be instantiated multiple times. 1 Answer
Multiple Cars not working 1 Answer
Help needed on c# code which all of you will find basic apart from me 1 Answer
Parent/Player teleports but its children dont teleport aswell 4 Answers