x


Best practice for parenting in code

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.

Transform transformToParentTo;
GameObject partToParent = Object.Instantiate() as GameObject;
Vector3 defaultScale = partToParent.transform.localScale;
partToParent.transform.parent = transformToParentTo;          
partToParent.transform.localPosition = Vector3.zero;
partToParent.transform.localRotation = Quaternion.identity;
partToParent.transform.localScale = defaultScale;

Is there a better way to be doing this?

Thanks

more ▼

asked Jun 17 '12 at 06:38 AM

altra4u gravatar image

altra4u
40 1 6 8

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.

Jun 17 '12 at 07:14 AM whydoidoit

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 ...

aa.inertiaTensor = Vector3.zero;
aa.inertiaTensorRotation = Quaternion.identity;

aa.velocity = Vector3.zero;
aa.angularVelocity = Vector3.zero;

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.

Jun 17 '12 at 08:16 AM Fattie

@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.

Jun 17 '12 at 05:45 PM Wolfram

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

Jun 18 '12 at 09:55 PM Wolfram

It's Mavis Beacon typing tutor that does it for me :)

That and 3 monitors :)

Jun 18 '12 at 10:05 PM whydoidoit
(comments are locked)
10|3000 characters needed characters left

0 answers: sort voted first
Be the first one to answer this question
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x138

asked: Jun 17 '12 at 06:38 AM

Seen: 378 times

Last Updated: Jun 20 '12 at 01:30 AM