- Home /
Position of Instantiated Objects from a reference
Hi, I want to instantiate objects with an equal distance from a reference
So for example if my reference is situated in the (0,0,0) and if I have 1 obj, then it's center will be in the position of the reference.
if i have 2 objects than, the sum of then they will also place themselves auto. according to the reference.
Here is the example
If I have understood correctly, you want to set the position during instantiating? http://docs.unity3d.com/ScriptReference/Object.Instantiate.html
What exactly do you need? Do you need logic? Do you have any code you've written that you can put here?
Okay, if i understood correctly then u want your instantiated object a specific distance away from an object, or the previous object instantiated, and they will instantiate both ways along the reference point.
If that is so, then var refPos : Vector3; var noOfObjects : int;
function Instantiate (){
var x : Transform;
x = Instantiate( OBJ , refPos , Quaternion.identity );
noOfObjects ++;
x.transform.position = THE DISTANCE BETWEEN THE OBJECTS/noOfObjects)
}
i didnt test this and u since i dont know what u want i cant give u a more specific script
@$$anonymous$$anco Capac, you are right I wanna do what you told.
@for all, I know how to instantiate objects, I just need this algorithm of finding the middle of sum of lengths of nr of objects I have div by 2, and to put them on both sides from reference (as I told for example 0,0,0.
So if I have one object it will be placed on , 0,0,0 .
if I have 2 objects they will be placed, one at -1, 0, 0 the second one at 1,0,0.
Something like this.
Answer by Noob_Vulcan · Jul 04, 2014 at 10:33 AM
Well just to be very simple ...You can make prefab of these 2 , 3 and 4 obj , and instantiate them . Center of all the Objects will be at instantiation position.
I hope this is want u want
private List<GameObject> item = new List<GameObject>();
public GameObject itemPrefab;
int numberofObjects = 0;
public float distance;
void Start ()
{
for(float i = 0 ; i <5 ; i++)
{
numberofObjects ++;
item.Add (Instantiate(itemPrefab, new Vector2(0.0f,2.0f), Quaternion.identity) as GameObject);
distance = Vector3.Distance (item[0].transform.position, item[numberofObjects -1].transform.position);
float temp = distance/numberofObjects;
}
}
I got something like this for now, I have the distance, but as you can see Im getting the distance after every iteration.
Now I need to place them and solve that distance .
Any Ideeas?