Question by
NasuaNarica · Dec 08, 2020 at 01:27 PM ·
spawningspawnpointsrandomspawning
How to make objects contantly spawn from multiple points
I'm making an infinite runner where you are a car dodging other cars on a 4 lane road. I was able to set up 4 spawn points so the obstacle cars spawn on each individual lane but my code only allows one car to spawn at a time. I would like 1-3 lanes of cars to spawn each time. How would i go about spawning multiple lanes of car per spawnInterval? Also my code is a a mess any tips would be appreciated on how to clean it up. I'm new to this so thank you so much for any and all advice!
public Transform[] spawnPoints;
public GameObject[] carPrefabs;
private float startDelay = 0f;
private float spawnInterval = 0.3f;
// Start is called before the first frame update
void Start()
{
//Repeats the SpawnVehicle method
InvokeRepeating("SpawnVehicles", startDelay, spawnInterval);
}
// Update is called once per frame
void Update()
{
}
//Get random number of spawnpoints and location of spawnpoints
public Transform GetSpawnPoints()
{
int index = Random.Range(0, spawnPoints.Length);
return spawnPoints[index];
}
//get random vehicle type
public GameObject GetVehicle()
{
int index = Random.Range(0, carPrefabs.Length);
return carPrefabs[index];
}
//spawn selected vehicle on selected spawnpoint(s)
public GameObject SpawnVehicles()
{
Transform spawnpoint = GetSpawnPoints();
GameObject Vehicle = GetVehicle();
GameObject v = Instantiate(Vehicle, spawnpoint.position, spawnpoint.rotation) as GameObject;
return v;
}
}
Comment