- Home /
children doesn't take parent's rotation, position or scale.
So as the title says, I have some gameobjects I want to be children of other gameobjects. However, changing their transforms don't work. The only one that works is the localScale one, but it makes the child have a different size from the one they have when they are not a child.Their rotation is completely random at times, as the inspector says the child is at 0,0,0 when it visually changed by 90º on the X axis. Any help on how to make them take the parents position, rotation or scale is appreciated.
This here is my code.
private void OnTriggerEnter(Collider col)
{
if ((col.CompareTag("RopaClara") || col.CompareTag("RopaOscura")) && !Ocupado_1)
{
col.transform.SetParent(pos_1);
col.transform.localPosition = new Vector3(0, 0, 0);
col.transform.rotation = pos_1.rotation;
//col.transform.localScale = new Vector3(5f, 5f, 5f);
Debug.Log(col.transform.position);
Ocupado_1 = true;
if (!Ocupado_2)
{
col.transform.SetParent(pos_2);
col.transform.localPosition = pos_2.transform.localPosition;
col.transform.localScale = new Vector3(2f, 2f, 2f);
Debug.Log(col.transform.position);
Ocupado_2 = true;
}
Can you try using col.gameObject.transform
instead of col.transform
? Just to make sure that you actually establish parent-child relationship between pos_1, or pos_1 and the GameObject that col is attached to.
Answer by Quelfth · Oct 28, 2020 at 05:00 PM
Transform.position
and Transform.rotation
are the absolute position and rotation of the transform. Transform.localPositon
, Transform.localRotation
and Transform.localScale
are their position, rotation, and scale, relative to their parent. So, when you said col.transform.localPosition = pos_2.transform.localPosition
, that's going to be setting the position of col
relative to pos_2
to whatever pos_2
's location is relative to it's parent. I can't imagine this is intended, since even assuming that pos_2
doesn't have a parent, and that it isn't rotated, col
's position will end up being double pos_2
's position is in world space.
If you want a transform to be at the same position as its parent, do
Transform.localPosition = Vector3.zero
.
If you want it to be the same rotation as its parent, do
Transform.localRotation = Quaternion.identity
.
And if you want it to be unrotated in world space, do
Transform.rotation = Quaternion.identity
.
If any of that doesn't seem to be doing what I said it would, you should make sure that the parent object isn't rotated or scaled, and that none of its parents are rotated or scaled.
If any of the parents in the hierarchy have rigidbodies, it's possible they're being rotated without your knowledge, and you may want to find and check the restraint check boxes on the rigid body.
Hi @Quelfth : whose transform is returned when calling Collider.transform? Is it the Collider object's own transform, or the transform of the GameObject instance that the Collider object is a part of?
Thank you very much for the answers! What I currently have is a box, with a gameobject child which also has all the pos_x as children, and each one of those pos_x needs to be able to have at least one more child, and these last children are the ones I need to set the rotation, position and scale.