12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364 |
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- namespace TrafficSimulation{
- public class CarSpawner : MonoBehaviour
- {
- public CarPool carPool;
- public TrafficSystem trafficSystem;
-
- public float carSpacing;
- public float carSize = 4.0f;
- public float bufferZone = 3.0f;
- // Start is called before the first frame update
- void Start()
- {
- foreach(Segment segment in trafficSystem.segments){
- spawnOnSegment(segment);
- }
- }
-
- void spawnOnSegment(Segment segment){
- if(segment == null){
- Debug.Log("Segment is null");
- return;
- }
- if(segment.waypoints.Count <= 2){
- Debug.Log("Waypoints not set");
- return;
- }
- float distance = 0.0f;
- Waypoint lastWp = null;
- foreach(Waypoint wp in segment.waypoints)
- {
- if(lastWp != null)
- {
- distance += Vector3.Distance(lastWp.transform.position, wp.transform.position);
- float wpDistance = Vector3.Distance(lastWp.transform.position, wp.transform.position);
- wpDistance = Mathf.Floor(wpDistance);
- float spawnDistance = wpDistance;
- spawnDistance -= bufferZone;
- while(spawnDistance >= bufferZone)
- {
- Vector3 spawnPos = lastWp.transform.position;
- Quaternion spawnRot = lastWp.transform.rotation;
- Vector3 currentRoad = wp.transform.position - lastWp.transform.position;
- spawnPos += (currentRoad.normalized * (wpDistance - spawnDistance));
- carPool.SpawnCar(spawnPos,spawnRot,wp.transform);
- Debug.Log("spawn Car on Segment " + segment.name + " and Waypoint " + lastWp.name + " in Distance " + (wpDistance - spawnDistance));
- spawnDistance -= carSize;
- spawnDistance -= carSpacing;
- }
- }
- lastWp = wp;
- }
- Debug.Log("Segment " + segment.name + "distance " + distance );
- //Transform spawnPos = segment.waypoints[0].transform;
- //spawnPos.LookAt(segment.waypoints[1].transform);
- //carPool.spawnCar(spawnPos);
- }
- }
- }
|