- Home /
Instantiate GameObject on local axis rather than world axis
I have a script that generates GameObjects in specific positions from an original GameObject (that one which the script is attached to) at (0,0,0) on the world axis. If I set the spawn location in the script to (10, 0, 0) and then move the GameObject to (34, 5, 10), how can I make it so the script will instantiate objects at (44, 5, 10) rather than (10, 0, 0)?
Here is the line in my code that determines one of the spawn locations of a new object,
var eastinstance : GameObject = Instantiate(eastroom, Vector3(0,0,12), Quaternion.Euler(0,180,0));
Thank you.
Answer by DaveA · Sep 19, 2012 at 10:54 PM
Assuming you are not parenting the new objects to this 'original' game object:
var eastinstance : GameObject = Instantiate(eastroom, transform.position+Vector3(0,0,12), Quaternion.Euler(0,180,0));
Thank you! That works but I forgot to take rotation into consideration, how would I handle that properly?
I believe you could do something like this,
var eastinstance : GameObject = Instantiate(eastroom, transform.position+Vector3(0,0,12), Quaternion.Euler(0,180,0));
var eastinstanceTransform = eastinstance.transform;
var offset : Vector3 = eastinstanceTransform.position - transform.position;
var rotOffset : Vector3 = offset * transform.rotation;
eastinstanceTransform.position = transform.position + rotOffset;
Or use the transform.forward*12.
Instantiate(eastroom, transform.forward * 12, transform.rotation+Quaternion.Euler(0,180,0));
Not tested!
Your answer
![](https://koobas.hobune.stream/wayback/20220613081238im_/https://answers.unity.com/themes/thub/images/avi.jpg)