- Home /
The question is answered, right answer was accepted
How to avoid scaling heritage when parenting.
Hello everyone. Have you ever tried to parent a gameObject un under a scaled father ? The scaling in herited, and your GameObject is scaled with weird scaling parameters. But how setup a child under a scaled parent without impacting the scale ? Try this context:
GameObject myCubeAsParent = GameObject.CreatePrimitive(PrimitiveType.Cube);
myCubeAsParent.transform.Translate(1,2,3);
myCubeAsParent.transform.Rotate(25,45,65);
myCubeAsParent.transform.localScale = new Vector3(2,0.5f,5);
GameObject mySphereAsChild = GameObject.CreatePrimitive(PrimitiveType.Sphere);
mySphereAsChild.transform.position = myCubeAsParent.transform.position;
mySphereAsChild.transform.parent = myCubeAsParent.transform;
As a start, I found that: Instead of parenting, let's match the roation of the parent first:
mySphereAsChild.transform.rotation = myCubeAsParent.transform.rotation;
mySphereAsChild.transform.parent = myCubeAsParent.transform;
It works ! Now The scale is preserved, but the gameboject is rotated as the parent, so now I need to reset the rotation to (By rotating in UI, I found these empiric values : (350,25,300) ) Anyone have some clues to findout how to restore/preset basic transform values while parenting ?
Answer by sneftel · Jul 24, 2011 at 03:58 PM
It's not clear what elements of the parent's transform you want inherited by the child, and what elements you don't, so I can't give you a very exact answer. I will tell you, though, that parenting an object to a parent with a non-uniform scale will generally give you results that you weren't expecting. If you want to scale the parent mesh, it's a better idea to make the two meshes siblings, and put the scale in the mesh node instead of the (new) parent node.
Thanks for answering. What do you mean by "put the scale in the mesh node" ? Do you mean I have to move the vertex and keep my scale at Identity(1,1,1) ?
No, I mean put the scale in the object with the mesh, not the object with children.
Ben 12 has right, we have to avoid non-uniform scale at any costs !
I solve it creating a clone mesh (without scale) and just matching each vertex of the first mesh. So, the new mesh will have the scale of (1,1,1) and the vertex will ba where you want them.
A tricky way of doing $$anonymous$$ayas's "Freeze transformations".
Answer by tyjkenn · Mar 15, 2013 at 03:11 AM
Create an empty GameObject. It has a scale of 1x1x1. Assign the scaled object as the parent. Then assign the empty GameObject as the parent to your desired child object. Now the Hierarchy should look like this:
MyCubeAsParent
EmptyGameObject
MySphereAsChild
I tried this, and somehow it worked. Apparently it only scales the object according to the immediate parent's local scale. The code looked something like this:
var emptyObject = new GameObject();
emptyObject.transform.parent = myCubeAsParent.transform;
mySphereAsChild.transform.parent = emptyObject.transform;
I have been searching for an answer for this for a long time and this worked perfectly for me. Thanks tyjkenn!!!
The method that I used that was inspired by this answer was to build my hierarchy using non-scaled empty game objects and then hanging the meshes off of each node as appropriate. $$anonymous$$g. with a segmented character mesh your empty game objects would be shoulder, elbow, and wrist but your mesh pieces would be upper arm, lower arm, and hand. The upper arm is scaled as you want (which you might do if you're using primitives like the cylinder) and parented to the shoulder, the elbow is also parented to the shoulder and just offset however you wish. Then the wrist and the lower arm are parented to the elbow.
This worked as a charm, quick and easy solution. Thanks tyjkenn!
Answer by jbat100 · Feb 16, 2017 at 02:00 PM
You can store the scale before before parenting then reapply it, did the trick for me
Vector3 scale = mySphereAsChild.transform.localScale;
mySphereAsChild.transform.parent = myCubeAsParent.transform;
mySphereAsChild.transform.localScale = scale;