- Home /
Relative position?
Hello. When I am using an instantiate script (or anything with a transformation) how can I make it relative to the object that the script is attached to, instead of being global (so that the overall object could move while things are being spawned within it, for example). Thanks
// Instantiates a prefab in a grid
var prefab : GameObject; var gridX = 5; var gridY = 5; var spacing = 2.0;
function Start () { for (var y = 0; y < gridY; y++) { for (var x=0;x<gridX;x++) { var pos = Vector3 (x, 0, y) * spacing; Instantiate(prefab, pos, Quaternion.identity); } } }
Answer by _Petroz · Dec 20, 2010 at 05:58 AM
Use transform.parent to make one transform relative to another. Then use localPosition and localRotation to set the position/rotation relative to the parent transform.
newObject.transform.parent = gameObject.transform; // make the new object a child of current project
How would I apply that to the script I posted above, so that after the objects are instantiated in the grid, the grid can move as a whole?