InstantiatePrefab.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1.  /*
  2. * InstantiatePrefab.cs must be run before any other custom scripts because
  3. * humanGameObject, wanderTimer and waypointsArray may be needed in other scripts.
  4. * This can be set in Uniy3d under "Edit -> Project Settings ... -> Script Execution Order".
  5. */
  6. using UnityEngine;
  7. using UnityEngine.AI;
  8. public class InstantiatePrefab : MonoBehaviour
  9. {
  10. [Header("Game Objects")]
  11. public GameObject prefabHuman;
  12. [SerializeField]
  13. private GameObject waypoints;
  14. [Header("Number of Humans and Groups")]
  15. public int amount = 25;
  16. public int maxNumPerGroup = 5;
  17. [Header("Set Spawn point")]
  18. public Vector3 spawnPoint;
  19. [Header("Steering")]
  20. public Vector2 speedMinMax = new Vector2(4.0f, 7.0f);
  21. public Vector2 wanderTimerMinMax = new Vector2(10.0f, 15.0f);
  22. public float stoppingDistance = 4;
  23. //Can be passed on
  24. [HideInInspector]
  25. public GameObject[][] humanGameObject;
  26. [HideInInspector]
  27. public Animator[][] humanAnimator;
  28. [HideInInspector]
  29. public NavMeshAgent[][] humanNavMeshAgent;
  30. [HideInInspector]
  31. public float[] wanderTimer;
  32. [HideInInspector]
  33. public Transform[] waypointsArray;
  34. // Start is called before the first frame update
  35. void Start()
  36. {
  37. //Save all waypoints from GameObject into Transform array
  38. Transform[] wpArray = waypoints.GetComponentsInChildren<Transform>();
  39. waypointsArray = new Transform[wpArray.Length - 1];
  40. for(int k = 1; k < wpArray.Length; ++k)
  41. {
  42. waypointsArray[k - 1] = wpArray[k];
  43. }
  44. //gameObject.GetComponent<WanderingAITest>().waypoints = waypointsArray;
  45. //Calculate number of groups
  46. int numGroups = 1;
  47. if(amount > 0)
  48. numGroups = (int) Mathf.Ceil((float)amount / (float)maxNumPerGroup);
  49. //Set size of attributes that can be passed on
  50. humanGameObject = new GameObject[numGroups][];
  51. humanAnimator = new Animator[numGroups][];
  52. humanNavMeshAgent = new NavMeshAgent[numGroups][];
  53. //Set wanderTimer
  54. wanderTimer = new float[humanGameObject.Length];
  55. int humansLeft = amount;
  56. //for positioning the instances
  57. float inRow = Mathf.Ceil(Mathf.Sqrt(amount));
  58. int count = 0;
  59. int offsetx = 0;
  60. int offsetz = 0;
  61. // instantiate humans, save in array and modifie
  62. for(int i = 0; i < humanGameObject.Length; ++i)
  63. {
  64. //Calculate Group Size
  65. int groupSize = (humansLeft > maxNumPerGroup) ? maxNumPerGroup : humansLeft;
  66. humanGameObject[i] = new GameObject[groupSize];
  67. humansLeft -= groupSize;
  68. //Set Animator and NavMeshAgent
  69. humanAnimator[i] = new Animator[groupSize];
  70. humanNavMeshAgent[i] = new NavMeshAgent[groupSize];
  71. //Nav Mesh Agent - speed
  72. float speed = Random.Range(speedMinMax.x, speedMinMax.y);
  73. //Wandering AI Waypoints (Script)
  74. wanderTimer[i] = Random.Range(wanderTimerMinMax.x, wanderTimerMinMax.y);
  75. //target scattering
  76. // instantiate humans per group and save them in matrix
  77. for (int j = 0; j < humanGameObject[i].Length; ++j)
  78. {
  79. // Instantiate human [i][j]
  80. humanGameObject[i][j] = Instantiate(prefabHuman, new Vector3(spawnPoint.x - offsetx, 0, spawnPoint.z - offsetz), Quaternion.identity, this.transform);
  81. // Calculate offset
  82. count++;
  83. if(offsetx < inRow * 2)
  84. {
  85. offsetx++;
  86. offsetx++;
  87. }
  88. else
  89. {
  90. offsetx = 0;
  91. offsetz++;
  92. offsetz++;
  93. }
  94. // Set Humans initial values
  95. humanGameObject[i][j].tag = this.transform.tag;
  96. humanGameObject[i][j].GetComponent<NavMeshAgent>().stoppingDistance = stoppingDistance;
  97. humanGameObject[i][j].GetComponent<NavMeshAgent>().speed = speed;
  98. humanGameObject[i][j].GetComponent<Animator>().speed = 0.5f + (speed / speedMinMax.y);
  99. // Set Components
  100. humanAnimator[i][j] = humanGameObject[i][j].GetComponent<Animator>();
  101. humanNavMeshAgent[i][j] = humanGameObject[i][j].GetComponent<NavMeshAgent>();
  102. }
  103. }
  104. }
  105. }