- Home /
How to Add Y Axis Offset to transform.localPosition on an Instantiated Prefab?
Hi,
Can somebody help clarify how to add an offset to the Y Axis on an instantiated prefab?
I have:
Instantiate(myPrefab,myPrefab.transform.localPosition,Quaternion.identity);
I need to add about a positive 2 or 3 to the Y Axis.
Any help is really appreciated.
Thanks!
Answer by Veldars · Jul 14, 2015 at 11:52 AM
Hi,
In your case I see two way to do it.
The first (and better for me) :
Instantiate(myPrefab, myPrefab.transform.localPosition + (myPrefab.transform.up * 2), Quaternion.identity);
The second :
Instantiate(myPrefab,new Vector3(myPrefab.transform.localPosition.x, myPrefab.transform.localPosition.y + 2, myPrefab.transform.localPosition.z),Quaternion.identity);
I hope this help.
You helped a bunch, thank you. I wasn't clear on the syntax.
I'm brand new to Unity and this was precisely the code I was looking for. Thanks!
Answer by Rhillion · Jul 14, 2015 at 01:24 PM
Vector3 offset = new Vector3(0, 2, 0);
Instantiate(myPrefab, myPrefab.transform.localPosition + offset, Quaternion.identity);
Your answer
Follow this Question
Related Questions
prevent object from falling to the left side when instantiated (spawned) ? 1 Answer
Stop Children of Instantiated Parent From Offsetting After Changing Position 0 Answers
Prefabs dont instantiate directly on point 0 Answers
Referencing gameObject from script after Instantiate 0 Answers
Instantiating one prefab with different materials in one frame 0 Answers