Hello,
Please telm me how to spawn objects with distance from each other?
p.s. I searched it but i didn t find anything that helps me and my game is 2d
Hi, this script will spawn the objects on the same y position with a distance of 2 in the x axis.
public GameObject objectToSpawn;
public int objectCount;
public int xPosition;
public int Distance;
void Start()
{
objectCount = 5;
xPosition = 0;
Distance = 2;
for (int i = 0; i < objectCount; i++)
{
Instantiate(objectToSpawn, new Vector2(xPosition, 0), Quaternion.identity);
xPosition += Distance;
}
}
For y axis spawning:
public GameObject objectToSpawn;
public int objectCount;
public int yPosition;
public int Distance;
void Start()
{
objectCount = 5;
yPosition = 0;
Distance = 2;
for (int i = 0; i < objectCount; i++)
{
Instantiate(objectToSpawn, new Vector2(0, yPosition), Quaternion.identity);
yPosition += Distance;
}
}