Instantiate weird position

Hi,
I think i don’t really get the transform position mechanisms :
On my scene I got a hierarchy like this:
player/shieldPos

with shieldPos as an empty game object that i’d like to use as a position for my shield to load
Now I’d like to instantiate a shield at this very same shieldPos place:
So in my player class i got

   publicvoidloadShield(strings){
GameObjectcloned= Instantiate(Resources.Load(s),newVector3(0,0,0), Quaternion.identity)asGameObject;
cloned.transform.parent = this.gameObject.transform.Find ("shieldPos");
cloned.name = "shield";
}

now I got a shield object as a child of ShieldPos (as I wanted), but not a the 0,0 pos as I wanted
the player is at pos(-1.97,0.697)
the ShieldPos is at pos(0.56,-.06) (of the player)
and my new instantiate shield is at (-2.53,0.637) of the shield pos, instead of (0,0) ?
So i see that i does a difference between the playerpos and the shieldPos, but how can i tell it to just stay at 0,0 ?

After setting the parent of ‘cloned’ object to ‘shieldPos’, you want to add this line :

cloned.transform.localPosition = Vector3.zero;

What it does is, make ‘shieldPos’ parent of the clone, and then set local position to (0,0,0) relative to the parent!

2 Likes

ok thanks; still a bit confusing to me with th local and world pos, but I’ll get use to it :wink:

Local position is your position in context of your parent. So if your object doesn’t have any parent, it’s local and world positions would be the same. Consider GameObjects A and B such that B is child of A and A doesn’t have a parent. If A’s position in inspector is (10,0,0) and B’s position is the same in inspector, it means that A is +10 units from world’s (0,0,0) in x-axis and B is +10 units from A in x-axis. Hence B is (10 + 10, 0, 0) relative to world.

1 Like

and now I’ve understand !
Thanks teach’ :slight_smile: