123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117 |
- using UnityEngine;
- using UnityEngine.AI;
- public class WanderingAITest : MonoBehaviour
- {
- public Transform[] waypoints;
- public float[] wanderTimer;
- public float targetScattering = 5.0f; //Streuung
- private float[] timer;
- private int[] prevWP;
- private Vector3[][] target;
- private GameObject[][] humansGO;
- private NavMeshAgent[][] humansNMA;
- private Animator[][] humansA;
- // Use this for initialization
- void Start()
- {
- humansGO = gameObject.GetComponent<InstantiatePrefab>().humanObjs;
- humansNMA = new NavMeshAgent[humansGO.Length][];
- humansA = new Animator[humansGO.Length][];
- target = new Vector3[humansGO.Length][];
- timer = new float[humansGO.Length];
- wanderTimer = gameObject.GetComponent<InstantiatePrefab>().wanderTimer;
- prevWP = new int[humansGO.Length];
- // waypoints are set in InstantiatePrefab
- for(int i = 0; i < humansGO.Length; i++)
- {
- humansNMA[i] = new NavMeshAgent[humansGO[i].Length];
- humansA[i] = new Animator[humansGO[i].Length];
- target[i] = new Vector3[humansGO[i].Length];
- prevWP[i] = -1;
- for(int j = 0; j < humansGO[i].Length; j++)
- {
- humansNMA[i][j] = humansGO[i][j].GetComponent<NavMeshAgent>();
- humansA[i][j] = humansGO[i][j].GetComponent<Animator>();
- humansA[i][j].SetBool("isWalking", false);
- humansA[i][j].SetBool("isIdle", true);
- timer[i] = wanderTimer[i];
- }
- }
- }
- private void FixedUpdate()
- {
- for(int i = 0; i < humansGO.Length; i++)
- {
- timer[i] += Time.deltaTime;
- if (timer[i] >= wanderTimer[i])
- {
- if (waypoints.Length == 0) return;
-
- int currentWP = Random.Range(0, waypoints.Length);
- // Until previous and current WP are no longer the same, otherwise double waiting time for same WP
- while(prevWP[i] == currentWP)
- currentWP = Random.Range(0, waypoints.Length);
- prevWP[i] = currentWP;
- target[i][0] = checkTarget(waypoints[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", true);
- humansA[i][j].SetBool("isIdle", false);
- }
- timer[i] = 0;
- }
- else
- {
- timer[i] = wanderTimer[i];
- }
- }
- else
- {
- for (int j = 0; j < humansGO[i].Length; j++)
- {
- if (humansNMA[i][j].remainingDistance < humansNMA[i][j].stoppingDistance)
- {
- humansA[i][j].SetBool("isWalking", false);
- humansA[i][j].SetBool("isIdle", true);
- }
- else
- {
- humansA[i][j].SetBool("isWalking", true);
- humansA[i][j].SetBool("isIdle", false);
- }
- }
- }
- }
- }
- 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.
- // -3.6f is the only valid entry for y
- modifiedTarget = new Vector3(modifiedTarget.x, -3.6f, modifiedTarget.z);
- NavMeshHit navHit;
- NavMesh.SamplePosition(modifiedTarget, out navHit, 1.0f, -1);
- return navHit.position;
- }
- }
|