- Home /
parent Transform.position unexpected behaviour when changing position
Hello,
I'm having some trouble with an unexpected behaviour of changing the position of a gameobject.
I have a gameobject, called worldobject, which holds two components, WorldManager and WorldDrawManager. After the calculations of the world data is completed (i'm creating an icosphere), WorldManager calls WorldDrawManager to draw all world chunks (each face of the original icosahedron is a chunk) as new gameobjects whose parent is worldobject and position is Vector3.zero.
Now, everything works fine when the gameobject is at position (0,0,0), but when I change the position of said object (for example at 0,-100,0) the icosahedron gets STILL drawn around the (0,0,0) origin point. The chunk objects' positions are each shown as (0,100,0), even if I explicitly set them to Vector3.zero.
Inspecting worldobject before I start the game shows its gizmo at the correct position, but after I complete the world generation its gizmo is shown at (0,0,0) even if its position in the component is correctly set.
Is this a bug or am I doing something wrong?
Thanks in advance!
Answer by bpaynom · Feb 01, 2019 at 12:30 PM
Your are instantiating them using world position instead of your object relative position. You can
use transform.localPosition to set the position
calculate the desired position relative to the parent
positioning the parent at 0,0,0, then instantiate all objects, then move it again to wherever it was.
Your choice.
The first option worked like a charm, but now I'm curious, why using position ins$$anonymous$$d of localPosition didn't work? I thought it'd be fine.
Thank you for your answer tho, it helped a lot!
The values shown in the inspector are localPosition values. One way to see the world values of an object is to unparent it, so the "parent" of that object is the scene. Then, those X,Y,Z values are world position values. I know at first is a bit confusing, but it's the way most 3D softwares work.
This makes so much sense now, I didn't know that at all. Thank you!
Your answer
Follow this Question
Related Questions
Camera follow ball along cylinder 1 Answer
Position variable not updating 1 Answer
Updating localPosition(?) in a scaled transform parent 1 Answer
how to stop child object from twitching? 1 Answer
Controlling parent by Inputs 1 Answer