- Home /
How can I change rotation of parent independently without affecting angles and rotation of child ??
hey guys, I have a question about parent rotation independent of child. I have a object A which rotates 360 degree y axis using localEulerAngles, when ever a object B touch object A, A makes B its parent. Now object A moves with B ( in circle) which is absolutely right, but now object B transforms itself to another shape and changes its rotation as well. Is there any solution I can change rotation of parent independently without affecting angles and rotation of child ??
sheer !!!
well, don't make them parent/childs. or do it the other way around. or make them both the child of something else.
Answer by Kryptos · Oct 19, 2012 at 12:38 PM
The purpose of parent/child connection is to have each child dependent of its parent.
So either don't connect them like @Fattie suggested. Or if it is a special operation you can temporarily unparent the child, modify the parent and reparent:
// Get all direct children
Transform[] children = new Transform[this.transform.childCount];
int i = 0;
foreach(Transform child in this.transform)
{
children[i++] = child;
}
// Detach
this.transform.DetachChildren();
// Change parent transform
this.transform.position = v3NewPosition;
// Reparent
foreach(Transform child in children)
{
child.parent = this.transform;
}
children = null;
This or you could, counter rotate the child. However it would be rather tedious.
@Sonaten that and it won't be possible if scale was applied to the parent and/or the child.
edit: By "not possible" I meant: "very hard" because the inverse transform i.e. the calculation of an inverse matrix with rotation, translation and scaling is not straightforward.
True, all the counter messures you would have to take. You should really be clear on the object to object relationship you want when using parenting in your game structure.
Answer by SeuMadruguinha · May 02, 2020 at 06:32 AM
I found a solution for my case, I wanted to rotate 'A' without rotating 'B':
- Parent A
- Child B
Solution:
- Parent C
- Child A
- Child B
What I did was create a new object C that is the parent of A and B, now with the script on object C I can choose whether I want to rotate only child A without affecting object B or vice versa.