- Home /
The question is answered, right answer was accepted
Spawn distance
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
Can't you just use instantiate and definine the location of each object?
Hi, could you please specify if this is a 2D or 3D game. And if you want to spawn them all in the same direction or random directions.
$$anonymous$$y game is 2d and i want them all at same direction
Could you please post the code you are working with and show where it's failing?
Answer by MT369MT · Aug 07, 2018 at 05:55 PM
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;
}
}
Follow this Question
Related Questions
How to use raycasting to find distance between enemy and obstacle 1 Answer
Serious performance issues 0 Answers
spawn 3 object in a row at the same time ( unity ) 1 Answer
Help with Spawning & Destroying Background Objects in 2D 2 Answers
How to spawn my gameobjects and have it disappear after i click it 1 Answer