- Home /
setting parent as object warps the child object
I have two cubes, cube1 and cube2. Both cubes are the same size initially.
cube1's transform Rotation is 0,45,0 and Scale is 1,1,0.3.
cube2's transform Rotation is 0,0,0 and Scale is 1,1,1.
If I make cube2 cube1's child, cube2 warps (see image). I want cube2 to move with cube1 but not be warped. This doesn't happen if cube1's Scale is 1,1,1. How do I stop cube2 warping? I actually dynamically parent cube1 in code so would be happy with a code based solution.
After parenting:
Before Parenting: Thanks for any help!
Have you tried transform.localScale = Vector3.one;
to reset the scale back to 1,1,1 after setting the parent?
Good day.
Then i see i was wrong...
So on solution can be after parinting it, change its local scale again.
As pointed out by the OP the problem can occur when the scale of the parent object is not uniform. Try it by yourself.
Hello Hellium, But if you have 2 objects with dirrefent scales and make one child of the other, you will see in the inspector that local positions, scale and rotation will change, But its global posit, rot and scale should not change... Parenting and unparenting does not change the "appearence" of the objects..? I'm not right? I can not try it until this night so... to prevent cofusions if is not tre , my answers should be removed.
Thx Hellium!
I don't know if the following suggestion will be possible for you:
I would create a new empty gameObject, and parent both cubes to it. Then, move the empty gameObject ins$$anonymous$$d of the cube1.
Answer by vrjeans · Sep 13, 2018 at 08:43 AM
Hello
I've encountered this problem also. This happens because child gameobjects inherit all of their parents' scales and rotations. The simple solution is to just place both your gameobjects under an empty parent gameobject with the rotation (0, 0 ,0) and scale of (1, 1, 1) and then rotate and scale them individually. To make your cube2 follow cube1 you will have to move the empty parent gameobject and both your cubes will follow. Rotating the parent should rotate all children (they will all rotate around a common pivot point, as if they were all one object) but scaling the parent will result in weird warps as you already noticed.
As for a code solution:
var prarent = Instantiate(parentPrefab); // an empty gameobject prefab with nothing except a transform
var cube1 = Instatiate(cubePrefab); // your cube prefab
var cube2 = Instatiate(cubePrefab);
cube1.transform.parent = parent.transform;
cube2.transform.parent = parent.transform;
cube1.transform.rotation = Quaternion.Euler(0, 45, 0);
cube2.transform.rotation = Quaternion.Euler(0, 0, 0);
cube1.transform.localScale = new Vector3(1, 1, 0.3);
cube2.transform.localScale = new Vector3(1, 1, 1);
something like that. You could probably use one of the Instatiate overloads to make it nicer looking.
Thank you so much. I had been stuck on this for a couple of days! Parenting both to an 1,1,1 scaled empty works. Incidentally, I can also scale the empty (uniformly) and that works to scale both children.