123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125 |
- /*
- * InstantiatePrefab.cs must be run before any other custom scripts because
- * humanGameObject, wanderTimer and waypointsArray may be needed in other scripts.
- * This can be set in Uniy3d under "Edit -> Project Settings ... -> Script Execution Order".
- */
- using UnityEngine;
- using UnityEngine.AI;
- public class InstantiatePrefab : MonoBehaviour
- {
- [Header("Game Objects")]
- public GameObject prefabHuman;
- [SerializeField]
- private GameObject waypoints;
-
- [Header("Number of Humans and Groups")]
- public int amount = 25;
- public int maxNumPerGroup = 5;
-
- [Header("Set Spawn point")]
- public Vector3 spawnPoint;
-
- [Header("Steering")]
- public Vector2 speedMinMax = new Vector2(4.0f, 7.0f);
- public Vector2 wanderTimerMinMax = new Vector2(10.0f, 15.0f);
- public float stoppingDistance = 4;
-
- //Can be passed on
- [HideInInspector]
- public GameObject[][] humanGameObject;
- [HideInInspector]
- public Animator[][] humanAnimator;
- [HideInInspector]
- public NavMeshAgent[][] humanNavMeshAgent;
- [HideInInspector]
- public float[] wanderTimer;
- [HideInInspector]
- public Transform[] waypointsArray;
-
- // Start is called before the first frame update
- void Start()
- {
- //Save all waypoints from GameObject into Transform array
- Transform[] wpArray = waypoints.GetComponentsInChildren<Transform>();
- waypointsArray = new Transform[wpArray.Length - 1];
- for(int k = 1; k < wpArray.Length; ++k)
- {
- waypointsArray[k - 1] = wpArray[k];
- }
- //gameObject.GetComponent<WanderingAITest>().waypoints = waypointsArray;
- //Calculate number of groups
- int numGroups = 1;
- if(amount > 0)
- numGroups = (int) Mathf.Ceil((float)amount / (float)maxNumPerGroup);
- //Set size of attributes that can be passed on
- humanGameObject = new GameObject[numGroups][];
- humanAnimator = new Animator[numGroups][];
- humanNavMeshAgent = new NavMeshAgent[numGroups][];
- //Set wanderTimer
- wanderTimer = new float[humanGameObject.Length];
- int humansLeft = amount;
- //for positioning the instances
- float inRow = Mathf.Ceil(Mathf.Sqrt(amount));
- int count = 0;
- int offsetx = 0;
- int offsetz = 0;
- // instantiate humans, save in array and modifie
- for(int i = 0; i < humanGameObject.Length; ++i)
- {
- //Calculate Group Size
- 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];
- //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)
- {
- // Instantiate human [i][j]
- humanGameObject[i][j] = Instantiate(prefabHuman, new Vector3(spawnPoint.x - offsetx, 0, spawnPoint.z - offsetz), Quaternion.identity, this.transform);
- // Calculate offset
- count++;
- if(offsetx < inRow * 2)
- {
- offsetx++;
- offsetx++;
- }
- else
- {
- offsetx = 0;
- offsetz++;
- offsetz++;
- }
- // Set Humans initial values
- humanGameObject[i][j].tag = this.transform.tag;
- humanGameObject[i][j].GetComponent<NavMeshAgent>().stoppingDistance = stoppingDistance;
- humanGameObject[i][j].GetComponent<NavMeshAgent>().speed = speed;
- humanGameObject[i][j].GetComponent<Animator>().speed = 0.5f + (speed / speedMinMax.y);
- // Set Components
- humanAnimator[i][j] = humanGameObject[i][j].GetComponent<Animator>();
- humanNavMeshAgent[i][j] = humanGameObject[i][j].GetComponent<NavMeshAgent>();
- }
- }
- }
- }
|