I am trying to get my AI to prioritize multiple enemies based on many different factors.
Right now I have a pretty basic setup. When the AI see’s an enemy it adds it to a generic list “enemyList” of type gameobject.
I have a coroutine that iterates through each element of “enemyList” with a for statement.
I check each element of “enemyList” for a bunch of stuff like distance, health, weapon type being carried, etc. Based on those values, I generate a priority value which I store in a new generic list “priorityLevel” of type float.
This works for now but I see this work flow getting very messy quickly. And if I want to store more info. about each element of “enemyList”, I am making more lists. And I don’t want to add a new list for every piece of info. I gather when I iterate through each element of “enemyList”
Is there an easier way to store all the info about each element of “enemyList” in 1 place? So I can easily look up and compare info’s about each element of “enemyList”.
From my research dictionaries and list are the way to go but I am really struggling with the syntax. I think I am suppose to create a separate class for the multiple variables I want to store but am struggling with syntax.
Any help appreciated, thanks.
IEnumerator Priority()
{
while (enemyList.Count > 0)
{
for (int i = 0; i < enemyList.Count; i++)
{
priorityLevel.Add(0f);
distance = Vector3.Distance(transform.position, enemyList*.transform.position);*
_ priorityLevel += distance;_
_ rifleCheck = enemyList*.GetComponent<Animator().GetBool(“RifleState”);
if (rifleCheck == true)
{
priorityLevel += 10.0f;
}
}*_
* yield return new WaitForSeconds(2f);*
* }*
* }*
I think what you want for this is a mini class as you mentioned. Can also use a struct, which is basically a stripped down class, however changing variables inside is not as easy (structs require using temp variables).
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class EnemyTracker : MonoBehaviour
{
public class StatsEnemy
{
public GameObject obj;
public Enemy script;
public Animator anim;
public float priority;
//Constructor, can use to set variables when using 'new StatsEnemy()'
public StatsEnemy(Enemy newEnemy)
{
script = newEnemy;
obj = script.gameObject;
anim = obj.GetComponent<Animator>();
priority = 0f;//All variables need to be set to something in a constructor
}
}
public List<StatsEnemy> enemyList = new List<StatsEnemy>();
void Start()
{
Enemy target = FindObjectOfType<Enemy>();
enemyList.Add(new StatsEnemy(target));
StartCoroutine(Priority());
}
IEnumerator Priority()
{
while (enemyList.Count > 0)
{
for (int i = 0; i < enemyList.Count; i++)
{
enemyList*.priority = 0f;*
_ float distance = Vector3.Distance(transform.position, enemyList*.obj.transform.position);_
_ enemyList.priority += distance;*_
_ if(enemyList*.anim.GetBool(“RifleState”) == true)
{
enemyList.priority += 10.0f;
}
}*_
* yield return new WaitForSeconds(2f);*
* }*
* }*
}