소스 검색

started with waitingTimer (WIP)

Furkan Karakocaoglu 2 년 전
부모
커밋
cf9119b380

+ 10 - 3
testumgebung/CrowdModelling/Assets/Depictions_Years/Scripts/General/InstantiatePrefab.cs

@@ -41,6 +41,8 @@ public class InstantiatePrefab : MonoBehaviour
     [HideInInspector]
     public float[] wanderTimer;
     [HideInInspector]
+    public List<float>[] waitingTimer; // waitingTime (1-5 s) X WPs
+    [HideInInspector]
     public Transform[] waypointsArray;
 
     // Start is called before the first frame update
@@ -86,6 +88,12 @@ public class InstantiatePrefab : MonoBehaviour
         //Set wanderTimer
         wanderTimer = new float[humanGameObject.Length];
 
+        // Set waitingTimer
+        waitingTimer = new List<float>[waypointsArray.Length];
+        for (int i = 0; i < waypointsArray.Length; ++i)
+            // create Matrix waitingTime X WPs with waitingTime from 1-5 seconds, bc we want all WP vistited the same duration
+            waitingTimer[i] = new List<float> { 1, 2, 3, 4, 5 };
+
         int humansLeft = amount;
 
         //for positioning the instances
@@ -105,7 +113,7 @@ public class InstantiatePrefab : MonoBehaviour
             int groupSize = (humansLeft > maxNumPerGroup) ? maxNumPerGroup : humansLeft;
             humanGameObject[i] = new GameObject[groupSize];
             humansLeft -= groupSize;
-        
+
             //Set Animator and NavMeshAgent
             humanAnimator[i] = new Animator[groupSize];
             humanNavMeshAgent[i] = new NavMeshAgent[groupSize];
@@ -113,10 +121,9 @@ public class InstantiatePrefab : MonoBehaviour
 
             //Nav Mesh Agent - speed
             float speed = Random.Range(speedMinMax.x, speedMinMax.y);
-            
+
             //Wandering AI Waypoints (Script)
             wanderTimer[i] = Random.Range(wanderTimerMinMax.x, wanderTimerMinMax.y);
-            //target scattering
 
             // instantiate humans per group and save them in matrix
             for (int j = 0; j < humanGameObject[i].Length; ++j)

+ 67 - 22
testumgebung/CrowdModelling/Assets/Depictions_Years/Scripts/WanderingAI/WanderingAI.cs

@@ -10,6 +10,7 @@ public class WanderingAI : MonoBehaviour
     public Transform[] waypoints;
     public List<Transform> waypointsList;
     public float[] wanderTimer;
+    public List<float>[] waitingTimer;
     [Header("Scattering around the target")]
     public float targetScattering = 5.0f; //Streuung
     [Header("Leave Settings")]
@@ -40,6 +41,7 @@ public class WanderingAI : MonoBehaviour
         humansNMA = gameObject.GetComponent<InstantiatePrefab>().humanNavMeshAgent;
         humansA = gameObject.GetComponent<InstantiatePrefab>().humanAnimator;
         wanderTimer = gameObject.GetComponent<InstantiatePrefab>().wanderTimer;
+        waitingTimer = gameObject.GetComponent<InstantiatePrefab>().waitingTimer;
         waypoints = gameObject.GetComponent<InstantiatePrefab>().waypointsArray;
         waypointsList = new List<Transform>(waypoints);
         humansPrio = gameObject.GetComponent<InstantiatePrefab>().humanPriorities;
@@ -61,41 +63,83 @@ public class WanderingAI : MonoBehaviour
         globalTimer = 0;
     }
 
+    //private void FixedUpdate()
+    //{
+    //    for(int i = 0; i < humansGO.Length; ++i)
+    //    {
+    //        if (timer[i] >= wanderTimer[i])
+    //        {
+    //            if (waypoints.Length == 0) return;
+
+    //            int currentWP = Random.Range(0, waypointsList.Count);
+    //            target[i][0] = CheckTarget(waypointsList[currentWP].transform.position, targetScattering);
+
+    //            if(target[i][0].x != float.PositiveInfinity)
+    //            {
+    //                for (int j = 0; j < humansGO[i].Length; ++j)
+    //                {
+    //                    target[i][j] = target[i][0];
+
+    //                    if (humansNMA[i][j].isActiveAndEnabled)
+    //                        humansNMA[i][j].SetDestination(target[i][j]);
+
+    //                    humansA[i][j].SetBool(isWalking, humansNMA[i][j].velocity.magnitude > 0.01f);
+    //                }
+    //                timer[i] = 0;
+    //                // remove from list to ensure that all WP are visited at the frequency indicated; if list empty fill list with initial WPs
+    //                ReduceWaypoints(currentWP);
+    //            }
+    //        }
+    //        else
+    //        {
+    //            for (int j = 0; j < humansGO[i].Length; ++j)
+    //            {
+    //                humansA[i][j].SetBool(isWalking, humansNMA[i][j].velocity.magnitude > 0.01f);
+
+    //                // if humans are not walking, then set priority to 0 so that the other humans dont disturb them
+    //                if(humansNMA[i][j].velocity.magnitude <= 0.01f)
+    //                    humansNMA[i][j].avoidancePriority = 0;
+    //                else
+    //                    humansNMA[i][j].avoidancePriority = humansPrio[i][j];
+    //            }
+    //        }
+    //        timer[i] += Time.deltaTime;
+    //    }
+    //}
+
     private void FixedUpdate()
     {
-        for(int i = 0; i < humansGO.Length; ++i)
+        for (int i = 0; i < humansGO.Length; ++i)
         {
-            if (timer[i] >= wanderTimer[i])
-            {
-                if (waypoints.Length == 0) return;
+            if (waypoints.Length == 0) return;
             
-                int currentWP = Random.Range(0, waypointsList.Count);
-                target[i][0] = checkTarget(waypointsList[currentWP].transform.position, targetScattering);
-
-                if(target[i][0].x != float.PositiveInfinity)
+            for(int j  = 0; j < humansGO[i].Length; ++j)
+            {
+                if(target[i][j] == Vector3.zero || 
+                    humansNMA[i][j].velocity.magnitude <= 0.01f || 
+                    humansNMA[i][j].remainingDistance <= humansNMA[i][j].stoppingDistance ||
+                    humansNMA[i][j].pathStatus == NavMeshPathStatus.PathComplete || 
+                    !humansNMA[i][j].hasPath)
                 {
-                    for (int j = 0; j < humansGO[i].Length; ++j)
+                    int currentWP = Random.Range(0, waypointsList.Count);
+                    target[i][j] = CheckTarget(waypointsList[currentWP].transform.position, targetScattering);
+
+                    if (target[i][0].x != float.PositiveInfinity)
                     {
-                        target[i][j] = target[i][0];
-                    
                         if (humansNMA[i][j].isActiveAndEnabled)
                             humansNMA[i][j].SetDestination(target[i][j]);
 
                         humansA[i][j].SetBool(isWalking, humansNMA[i][j].velocity.magnitude > 0.01f);
+
                     }
-                    timer[i] = 0;
                     // remove from list to ensure that all WP are visited at the frequency indicated; if list empty fill list with initial WPs
-                    reduceWaypoints(currentWP);
+                    ReduceWaypoints(currentWP);
                 }
-            }
-            else
-            {
-                for (int j = 0; j < humansGO[i].Length; ++j)
+                else
                 {
                     humansA[i][j].SetBool(isWalking, humansNMA[i][j].velocity.magnitude > 0.01f);
-
                     // if humans are not walking, then set priority to 0 so that the other humans dont disturb them
-                    if(humansNMA[i][j].velocity.magnitude <= 0.01f)
+                    if (humansNMA[i][j].velocity.magnitude <= 0.01f)
                         humansNMA[i][j].avoidancePriority = 0;
                     else
                         humansNMA[i][j].avoidancePriority = humansPrio[i][j];
@@ -103,9 +147,10 @@ public class WanderingAI : MonoBehaviour
             }
             timer[i] += Time.deltaTime;
         }
+
     }
 
-    public static Vector3 checkTarget(Vector3 target, float dist)
+    public static Vector3 CheckTarget(Vector3 target, float dist)
     {
         Vector3 modifiedTarget = Random.insideUnitSphere * dist + target;
         // SamplePosition also checks the y axis. However, this is not relevant for me, so valid positions (x & z) are also discarded.
@@ -118,7 +163,7 @@ public class WanderingAI : MonoBehaviour
         return navHit.position;
     }
 
-    public void reduceWaypoints(int index)
+    public void ReduceWaypoints(int index)
     {
         waypointsList.RemoveAt(index);
 
@@ -126,7 +171,7 @@ public class WanderingAI : MonoBehaviour
             waypointsList = new List<Transform>(waypoints);
     }
 
-    public void leaveMarket(int i)
+    public void LeaveMarket(int i)
     {
         for (int j = 0; j < humansGO[i].Length; ++j)
         {