- Home /
Object scale/rotation changes when parented to flipped object
Hello! I'm working on a project where user drawings are added to in-game objects, and we've come across an issue with changing transform.parent on game objects.
In this instance, the code will change the parent(s) of a list player-drawn line segments to the tree visible in the following screenshot when the line collides with a canvas attached to the tree:
This works as expected. Each line is positioned to the point where the input collides with the tree's 'canvas' mesh, and each line segment is rotated to face the next line segment with Transform.LookAt. When the parent of each segment is changed to the tree, the segments maintain their respective relative world position/rotation/scale.
However, our problem comes up when the target parent object (in this case, the tree) is scaled negatively on one or more axes before attaching the children. When changing the Transform.parent property on a line segment, if the parent tree is flipped, the lines go crazy and assume some seemingly random rotation/scale (seen below).
Just to clarify, I'm not talking about scaling the parent object after the children are already assigned to it. That works perfectly fine. But when a parent object has a negative scale (in this case, the tree has a scale of (-1,1,1)) and children are attached, the children don't maintain their world rotation/scale as expected.
Why does this happen? Is there any way to circumvent it? Any help is appreciated! Thanks in advance! :)
Edit: I've confirmed, the issue is very similar to the collider issue linked in my comment below. Here's an easy way to recreate my issue:
Start a new project and scene
Add 2 quads to the scene
Give them each a rotation of (20,20,20) (just so you can see the undesired effect)
Create 2 empty gameobjects, and give the 2nd a scale of (-1,1,1)
Move one quad into the first empty gameobject, and notice that the quad doesn't change shape. Move the 2nd quad into the 2nd empty gameobject, and notice that the quad rotates and scales in an unexpected way.
Edit #2: I found out what factor by which the lines are offset... Apparently when the parent is scaled negatively on X, the child needs an inverse Y and Z rotation, and the scale needs to be reset to the values before it was added to the parent. What a weird bug :/
I think this issue might be closely related to this bug, which is easily reproduced.
The idea is, create a cube in a new project. Add the cube to an empty game object as a child. Change the parent object's scale to (-1,1,1). Rotate the child cube, and observe the difference between its mesh and its bounding box.
$$anonymous$$aybe it's the same bug and just interacts with quads in a different way?
Your answer
Follow this Question
Related Questions
Make gameobject a child without affecting scale? 1 Answer
Problem with child and parent. 1 Answer
Good way to scale parent without scaling children? 1 Answer
Make a simple tree 1 Answer