I’m doing a AI weapon selector. Each weapon has a fitness value depending on the position of the nearest enemy. I want the AI to use the weapon with the highest fitness, but only if it is over 0.75 and the weapon has bullets left.
Here is my code.
using System.Collections;
public class AIWeaponInput : MonoBehaviour {
WeaponManager wM;
private const float DESITION_TIME = 0.2f;
private float temp;
public float viewRadius;
public Collider[] colliders;
public Vector3 nearestCarPos;
public float[] fitness;
private int[] sorted;
void Start(){
wM = GetComponent<WeaponManager>();
fitness = new float[wM.weaponList.Count];
}
void Update(){
temp -= Time.deltaTime;
if(temp < 0){
temp = DESITION_TIME;
UpdateData();
EvaluateFitness();
SelectWeapon();
}
}
private void UpdateData(){
colliders = Physics.OverlapSphere(transform.position, viewRadius, 1 << Layers.CAR);
if(colliders.Length > 1){
nearestCarPos = colliders[1].transform.root.position;
}
}
private void EvaluateFitness(){
for (int i = 0; i < wM.weaponList.Count; i++) {
fitness _= wM.weaponList*.CalculateFitness(nearestCarPos);*_
* } *
* }*
* private void SelectWeapon(){*
* //Help!*
* }*
}
Finding the one with the highest fitness its pretty easy, the thing is to find the second, or third if the ones with the better fitness don’t have bullets left.
I can’t bubble sort the fitness array because its positions are linked to the list of weapons in the weapons manager.
I thought of having an array of bools to check if I have already checked that weapon, but I don’t know how to implement it.