Ver código fonte

refined AI behaivour

Marcel Zickler 2 anos atrás
pai
commit
365ce986f3

+ 10 - 5
Assets/Scripts/TrafficSimulation/IntersectionSituation/CarSituationSpawner.cs

@@ -6,7 +6,7 @@ namespace TrafficSimulation{
     public class CarSituationSpawner : MonoBehaviour
     {
         public float spawnRate = 5.0f;
-        public Transform[] spawns;
+        public GameObject[] spawns;
         public PresetTrigger[] despawns;
         public CarPool carPool;
         private List<IEnumerator> coroutines = new List<IEnumerator>();
@@ -59,8 +59,9 @@ namespace TrafficSimulation{
         /// Start a coroutine to spawn cars at every given spawn point
         /// </summary>
         private void StartSpawns(){
-            foreach(Transform spawnPos in spawns){
-                IEnumerator coroutine = AutoSpawning(spawnPos);
+            foreach(GameObject spawn in spawns){
+                SpawnWatcher spWatch = spawn.GetComponent<SpawnWatcher>();
+                IEnumerator coroutine = AutoSpawning(spawn.transform,spWatch);
                 this.coroutines.Add(coroutine);
                 StartCoroutine(coroutine);
             }
@@ -98,11 +99,15 @@ namespace TrafficSimulation{
         /// </summary>
         /// <param name="position">Transform</param>
         /// <returns></returns>
-        public IEnumerator AutoSpawning(Transform position){
+        public IEnumerator AutoSpawning(Transform position,SpawnWatcher spWatch){
             while(true){
-                SpawnCar(position);
+                if (spWatch.spawnIsFree)
+                {
+                    SpawnCar(position);
+                }
                 yield return new WaitForSeconds(this.spawnRate);
             }
         }
+
     }
 }

+ 17 - 7
Assets/Scripts/TrafficSimulation/VehicleAI.cs

@@ -65,15 +65,19 @@ namespace TrafficSimulation {
 
         void Start()
         {
-            wheelDrive = this.GetComponent<WheelDrive>();
+            this.wheelDrive = this.GetComponent<WheelDrive>();
 
             if(trafficSystem == null)
                 return;
 
-            initMaxSpeed = wheelDrive.maxSpeed;
+            this.initMaxSpeed = wheelDrive.maxSpeed;
             SetWaypointVehicleIsOn();
         }
 
+        private void OnEnable()
+        {
+            SetWaypointVehicleIsOn();
+        }
         private void FixedUpdate()
         {
             if (trafficSystem == null)
@@ -173,12 +177,12 @@ namespace TrafficSimulation {
                         if (otherVehicle.maxSpeed < wheelDrive.maxSpeed && otherVehicle.maxSpeed > 0 && dotFront > .8f)
                         {
                             float ms = Mathf.Max(wheelDrive.GetSpeedMS(otherVehicle.maxSpeed) - .5f, .1f);
-                            //Debug.Log(obstacle.name + " slower. lower speed of " +gameObject.name + " to " + wheelDrive.GetSpeedUnit(ms) + "!");
+                            Debug.Log(obstacle.name + " slower. lower speed of " +gameObject.name + " to " + wheelDrive.GetSpeedUnit(ms) + "!");
                             wheelDrive.maxSpeed = wheelDrive.GetSpeedUnit(ms);
                         }
                         else if(otherVehicle.maxSpeed == 0f)
                         {
-                            //Debug.Log(gameObject.name + ": Stopping because of " + obstacle.name  +"!");
+                            Debug.Log(gameObject.name + ": Stopping because of " + obstacle.name  +"!");
                             acc = 0;
                             brake = 1;
                             wheelDrive.maxSpeed = 0f;
@@ -188,7 +192,7 @@ namespace TrafficSimulation {
                         //If the two vehicles are too close, and facing the same direction, brake the ego vehicle
                         if(hitDist < emergencyBrakeThresh && dotFront > .8f)
                         {
-                            //Debug.Log(gameObject.name + "Two close, same direction. Breaking");
+                            Debug.Log(gameObject.name + "Two close, same direction. Breaking");
                             acc = 0;
                             brake = 1;
                             wheelDrive.maxSpeed = Mathf.Max(wheelDrive.maxSpeed / 2f, wheelDrive.minSpeed);
@@ -273,13 +277,13 @@ namespace TrafficSimulation {
             for(float a=-initRay; a<=initRay; a+=raySpacing)
             {
                 CastRay(raycastAnchor.transform.position, a, this.transform.forward, raycastLength, out detectedObstacle, out hitDist);
-
                 if(detectedObstacle == null){
                     continue;
-                } 
+                }
                 float dist = Vector3.Distance(this.transform.position, detectedObstacle.transform.position);
                 if(dist < minDist) 
                 {
+                    Debug.Log("Detected Object: " + detectedObstacle.name);
                     minDist = dist;
                     break;
                 }
@@ -327,6 +331,12 @@ namespace TrafficSimulation {
 
         void SetWaypointVehicleIsOn()
         {
+            if(trafficSystem.segments == null || trafficSystem.segments.Count == 0)
+            {
+                Debug.Log("Segments are null or count is 0");
+                return;
+            }
+
             //Find current target
             foreach(Segment segment in trafficSystem.segments)
             {

+ 38 - 0
Assets/SpawnWatcher.cs

@@ -0,0 +1,38 @@
+using System.Collections;
+using System.Collections.Generic;
+using UnityEngine;
+
+public class SpawnWatcher : MonoBehaviour
+{
+    public bool spawnIsFree;
+    public List<string> consideredTags;
+
+    // Start is called before the first frame update
+    void Start()
+    {
+        this.spawnIsFree = true;
+    }
+
+    // Update is called once per frame
+    void Update()
+    {
+        
+    }
+    private void OnTriggerEnter(Collider other)
+    {
+        if (this.consideredTags.Contains(other.tag))
+        {
+            //Debug.Log("Spawn isn´t free because " + other.tag);
+            this.spawnIsFree = false;
+        }
+    }
+
+    private void OnTriggerExit(Collider other)
+    {
+        if (this.consideredTags.Contains(other.tag))
+        {
+            //Debug.Log("Spawn is free again because " + other.tag);
+            this.spawnIsFree = true;
+        }
+    }
+}

+ 11 - 0
Assets/SpawnWatcher.cs.meta

@@ -0,0 +1,11 @@
+fileFormatVersion: 2
+guid: 9d45988135bb3804e9c0ec977d989334
+MonoImporter:
+  externalObjects: {}
+  serializedVersion: 2
+  defaultReferences: []
+  executionOrder: 0
+  icon: {instanceID: 0}
+  userData: 
+  assetBundleName: 
+  assetBundleVariant: