Hello, dear community!
I would to make a bot be able to choose an enemy which gets close. So if an enemy enters the trigger - it gets chosen. However if there are many enemies which enter the trigger, how to choose them? I went the following path, maybe the wrong one, please suggest better if possible.
I decided to use array (I use C# and heard these arrays cannot be resized), so initially I create as big array as possible, - an array with length of all possible enemies (say 100). Then, when enemies enter, they got added to the array (somehow, the code is still not written), and then the most tricky part: when a bot randomly chooses an enemy, how to prevent it from choosing empty?
If there are many enemies the process can take considerable time. Is there any way to make it quicker? Or even the other way round?
Thank you very much!
void OnTriggerStay(Collider other){
//create an array of fixed length, length is max number of enemies
enemyarray = enemyarray [maxenemypossible]; //doesn't work
if (other.GetComponent<Collider>().tag == "enemy") {
//if there are more than 1 enemy inside the trigger, add all enemies into the array
if (other.gameObject > 1) {
//get number of enemies inside
//add them into the array
enemyarray = [for everyother.gameObject, add into array, till noenemies left inside the trigger]
//choose random enemy
enemy = Mathf.Random(0, enemyarray[].length);
//this can take much time to chose an enemy;
//if I have 10000 enemies and only 2 are inside;
//it can take (2/10000)/60FPS = 3 seconds time which is a lot
if(enemy == null){
enemy = Mathf.Random(0, enemyarray[].length);
}
//is theresomething like this:
//enemy = Mathf.Random(0, enemyarray[].length - allemptyelements);
//?
}
}
}