// Update is called once per frame void Update() { // Change traffic lights when time if (intersections.Count > 0) { foreach (RoadIntersection intersect in intersections) { intersect.CheckTimeAndIfReadyChangeLight(); } } // Add a new car to the road when time if (spawningCars && Time.time - carSpawnTimerRef > carSpawnRateActual) { // Select random car prefab from list and assign a random path on the road system GameObject randomCar = carPrefabs[Random.Range(0, carPrefabs.Count)]; currentPathToAssign = spawnPaths[Random.Range(0, spawnPaths.Count)]; // Check if any cars on the road are at the spawn point chosen for new car bool assignedPathIsBlocked = false; foreach (NPCVehicle veh in vehicles) { if (veh.GetWaypoint() == currentPathToAssign.roadWaypoints[0]) { assignedPathIsBlocked = true; } } // If the spawn point isn't blocked by any existing cars, put the new car there, add it to the list of cars, and restart timer if (!assignedPathIsBlocked) { GameObject newCar = Instantiate(randomCar, currentPathToAssign.roadWaypoints[0].position, Quaternion.identity); NPCVehicle newCarAI = newCar.GetComponent <NPCVehicle>(); newCarAI.SetPath(currentPathToAssign, 1); vehicles.Add(newCar.GetComponent <NPCVehicle>()); carSpawnRateActual = Random.Range(spawnRateSecPerCar - spawnRateDeltaInSec, spawnRateSecPerCar + spawnRateDeltaInSec); carSpawnTimerRef = Time.time; } } }
// Add a vehicle to the list of vehicles to be destroyed // Should be accessed by NPCVehicle scripts public void AddVehicleToDestroyList(NPCVehicle carToDestroy) { Destroy(carToDestroy.gameObject); vehicles.Remove(carToDestroy); }