- Home /
Non-hierarchical child-parent transformation
I have two objects, a parent object and a child object, the child object has a list of points that I use with a line renderer to visualize, this points don't follow the parent transforms, they are a list of Vector3. that I asign in run time, think of drawing on a sphere. and I want the drawing to move and rotate along with the spehre. I asign them with a raycast on the spehre.
for (int i = 0; i < pathPoints.Count; i++)
{
Quaternion rotation = transform.parent.transform.rotation;
Matrix4x4 m = Matrix4x4.TRS(transform.parent.position, rotation, Vector3.one);
pathPoints[i] = m.GetColumn(3);
}
This is what I have right now, with this, when I move the spehre (parent) the child object follows normally and the point from the child component follows too but goes to the parent's position. so that's a start, I just need the points to retain their local position and when the parent rotates moving them as if they were regular transform objects parented by hierarchy.
Answer by arttu94 · Sep 11, 2019 at 08:56 PM
Found an old similar question and now it works the current code I use is this
Vector3 startPathPosition;
void Start()
{
startPathPosition = pathPoints[0];
startPathPosition = DivideVectors(Quaternion.Inverse(transform.parent.rotation) * (startPathPosition - transform.parent.position), Vector3.one);
}
Vector3 DivideVectors(Vector3 num, Vector3 den) {
return new Vector3(num.x / den.x, num.y / den.y, num.z / den.z);
}
void Update()
{
if (pathPoints.Count > 0)
{
for (int i = 0; i < pathPoints.Count; i++)
{
Quaternion rotation = transform.parent.transform.rotation;
Matrix4x4 m = Matrix4x4.TRS(transform.parent.position, rotation, Vector3.one);
pathPoints[i] = m.MultiplyPoint3x4(startPathPosition);
}
}
}
it's hard coded to use the first point of the list I was using for reference to place a cube and see what was going on. but it works fine. scale is set to vector3.one as I don't change the scale of the parent object.
Your answer
![](https://koobas.hobune.stream/wayback/20220612212011im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Positioning problem when using hierarchy 1 Answer
Animating a mecanim avatar through code with Quaternions. 0 Answers
How to make the child follow the parent's rotation 0 Answers
Relative transforms and parenting 1 Answer
Constance Force Rotation not transferring to child object. How to fix? 0 Answers