- Home /
 
Logic Question with position and parenting
Hello,
I'm trying to move a child object of a "room" to be on one of the four walls of the room. The problem I'm having seems to be with the fact that the parent, or room, is scaled to fit a specific space, so for example, 12x6x8 (x,y,z), so when I tried to move the child based on the parent's size/extents with localPosition, it moves the child very far off.
If I manually move it, values like .2 come out to be half the room's size when moving the child, so if I move the child .2 it will be on the wall.
I'm having a hard time figuring out how I can move the child object precisely to the parent's walls when the scale of the room is messing with the localPosition values of the child.
Any tips on this?
Thank you for reading!
Answer by StewVanB · Jun 10, 2014 at 08:52 PM
I will try to explain how this works.
gameObject.localPosition is based on the parent object's "Perfect Zero"
Let's say that you have a parent object of 16,16,8 scale.
If you want the positive X wall you set the child's localPosition to 0.5
The math is like this: (Parent Object's Zero) + ((Parent Object's Scale) * (Child's localPosition))
So in our example: (0.0) + ((16.0)*(0.5)) = 8.0
This will place the child object's absolute zero at the parent's absolute zero + 8.0 or half of the scale.
Using this principle 0.5 in x,y or z will always place the child object at the edge(wall) or the parent object.
Hope this helps!
This fixed the issue I was having, and can now set the objects to the wall properly, thanks!
Answer by Kiwasi · Jun 10, 2014 at 08:53 PM
Multiple solutions, depending on the exact nature of your set up.
Unparent the object, move it, then reassign the parent
Assign the parent a scale of (1,1,1) (Model it properly so the dimensions match without scaling)
Assign the room at appropriate scale to an empty parent gameObject. Assign the child gameObject to the same empty parent.
Your answer