- Home /
How to make parent scale inversely affect child scale?
Hi. Hope this is quick since it seems like an easy question.
I have two objects in a parent/child hierarchy that I created by dragging one object onto another in the Unity editor then saving the whole thing as a prefab. The child object has a visual aspect to it, but not the object it is under.
In a new scene I am scaling the prefab in the X direction. This visually scales the child object as well. What I would like to do is have the child object detect how much the top object is scaled by in the X direction, then divide its own scale by that number.
So if I were to scale the prefab to 5 in the X direction, the child in the prefab would (upon playing the scene) divide its X scale by 5.
What do I put on the second object to have it find the first object's scale? I'm not sure if it is considered a true parent/child relationship in the javascript since I did not make that distinction in code. Once I have the scale of the "parent" I'm golden since the rest is just division.
Thanks!
Take a look into the scripting documentation, the Transform component is all you need ;) http://unity3d.com/support/documentation/ScriptReference/Transform.html
Answer by Bunny83 · Jan 13, 2011 at 03:22 AM
i guess you need this on your child object to get the parents scale:
transform.parent.localScale
I don't understand why i would scale a parent GO with no visual effect and want to keep the childs scale constant. Maybe just reverse the hierachy? Having the visual object outside and the one you want to scale inside don't produce that problem. If that's not possible you have to scale the child as you described above ;)
I was actually leaving out the use I have for the parent since I didn't think it important to what I was asking. While it has no visual aspect, it is used for something else and it needed to be the parent for the visual object. Thank you!