12345678910111213141516171819202122232425262728293031323334353637383940414243444546 |
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- public class CarSpawner : MonoBehaviour
- {
- public List<GameObject> Spawners;
- public List<GameObject> Cars;
- public float SpawnRate;
- private List<float> lastSpawn = new List<float>();
- // Start is called before the first frame update
- void Start()
- {
- Debug.Log("Start");
- for (int i = 0; i <= Spawners.Count; i++)
- {
- lastSpawn.Add(Time.time);
- }
- }
- // Update is called once per frame
- void Update()
- {
- int index = 0;
- foreach(GameObject spawn in Spawners){
- SpawnCar(spawn, index);
- index++;
- }
- }
- private void SpawnCar(GameObject spawn, int index)
- {
- if(lastSpawn[index] + SpawnRate > Time.time)
- {
- // No need for a new spawn
- return;
- }
- Debug.Log("Spawn!");
- int carIndex = Random.Range(0, Cars.Count);
- GameObject car = Cars[carIndex];
- GameObject newCar = Object.Instantiate(car, spawn.transform.position,spawn.transform.rotation);
- lastSpawn[index] = Time.time;
- }
- }
|