|
Hi guys I'm trying to parent an instantiated asset to another asset(also created at run time) The code I'm having to write right now does not make me happy and just trying to make sure I'm doing the best thing possible. Basically I have to set the localPosition to zero, localRotation to Identity and capture the scale before parenting to apply it after parenting. Terrible... Here is the code. Is there a better way to be doing this? Thanks
(comments are locked)
|

Well that's what I do. I wrote a function for it which does the scale optionally, because you don't always want that I find.
I thought I might mention, there are some situations (like if you re=use an object after sitting it offscreen) where you have to do this ...
conceivably you might have to care about that if you are sort of instantiating in to an already twisting, moving object and then sort of separating it at that point, something to consider.
@altra4u: Yup, that's the way to do it. We put that code into a static global function, so you don't have to retype/copypaste it whenever you use it. Also @whydoidoit's optional scaling is a good idea.
@Owen: Well, depends on what you're tyring to accomplish. If you want to keep the object at the same world coords, then obviously
A.parent=B;suffices. But most often you are instantiating some other object or prefab, which in itself is positioned at its "local" world coords origin. If you merely reparent that to a transformed object, the instance will still be placed at the world origin, when instead you want it placed relative to the parent, using it as its pivot - therefore you'll need to reset all local transformations after reparenting.Well, the default behaviour is the same as when you're reparenting in the Hierarchy view: the world coordinates stay the same, the local coordinates change. But I agree, it seems strange that there is no convenience function resetting the whole local matrix.
EDIT: damn, how come @whydoidoit is always faster than me? are you in a different time zone? ;-P
It's Mavis Beacon typing tutor that does it for me :)
That and 3 monitors :)