Question by
wolmer · Jan 13, 2016 at 10:27 AM ·
navmeshnavmeshagentdistancedistance check
How to get a list of the closest distance targets with NavMesh.
So we have an enemy, which is supposed to target the closest target available (the shortest path distance). But somehow this code only returns the distance of one path. Please help :)
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class AI : MonoBehaviour
{
//Public variables for testing purpose
public List<GameObject> enemies = new List<GameObject>();
public List<GameObject> sortedEnemies = new List<GameObject>();
public List<float> distances = new List<float>();
public List<float> sortedDistances = new List<float>();
protected GameObject target;
private NavMeshPath myPath = new NavMeshPath();
// Public for the sake of seeing it in Inspector
public float statPool = 10f;
public float movementSpeed;
public float enrageDistance = 2f;
private bool enrage = false;
public float attackSpeed; // Seconds before hit. aka 2 = 0.5 hits per second
public float attackCountdown;
void Start()
{
// Needs some kind of fixing, but I can't think of a smart one.
NavMeshAgent agent = GetComponent<NavMeshAgent>();
// rolling out random stats for the snail
// gonna need a static int with amount of "rounds"
statPool -= 3f; // This is to compensate for the minimum of 1 in each stat
//health = Random.Range(1, statPool); // statpool * "rounds" and prob divided by something
// damage = Random.Range(1, (statPool - health)); // -ll-
movementSpeed = 1 + (statPool /*- health - damage*/); // -ll-
agent.speed = movementSpeed;
}
void Update()
{
if (enrage == false)
{
Aim();
}
else
{
GetComponent<NavMeshAgent>().SetDestination(target.transform.position);
}
if (attackCountdown > 0)
{
attackCountdown -= Time.deltaTime;
}
}
void Aim()
{
//Clear old data.
enemies.Clear();
distances.Clear();
sortedEnemies.Clear();
sortedDistances.Clear();
target = null;
//Check for taunt
if (GameObject.FindGameObjectsWithTag("Taunt").Length > 0)
enemies = new List<GameObject>(GameObject.FindGameObjectsWithTag("Taunt"));
else
enemies = new List<GameObject>(GameObject.FindGameObjectsWithTag("PlayerRelated"));
//Set distance between every enemy and this unit.
for (int i = 0; i < enemies.Count; i++)
{
target = enemies[i];
GetComponent<NavMeshAgent>().SetDestination(target.transform.position);
Debug.Log("Distance between AI and " + target.gameObject.name + " is " + GetComponent<NavMeshAgent>().remainingDistance);
distances.Add(GetComponent<NavMeshAgent>().remainingDistance);
}
Sort(distances, enemies);
target = sortedEnemies[0];
if (target == null)
return;
GetComponent<NavMeshAgent>().SetDestination(target.transform.position);
//if (GetComponent<NavMeshAgent>().remainingDistance <= enrageDistance && target != null)
// enrage = true;
}
void Sort(List<float> floatList, List<GameObject> objectList)
{
float tempFloat;
GameObject tempObject;
bool sorted = false;
while (sorted == false)
{
sorted = true;
for (int i = 0; i < objectList.Count - 1; i++)
{
//Switch.
if (floatList[i + 1] < floatList[i])
{
tempFloat = floatList[i + 1];
tempObject = objectList[i + 1];
floatList[i + 1] = floatList[i];
objectList[i + 1] = objectList[i];
floatList[i] = tempFloat;
objectList[i] = tempObject;
sorted = false;
}
}
}
sortedDistances = floatList;
sortedEnemies = objectList;
}
}
Comment
Your answer
Follow this Question
Related Questions
Navmesh agent remaining distance wrong when movement done by root motion of Mecanim? 1 Answer
remaining distance strange bug 0 Answers
Why is my list bigger than it should be? 2 Answers
How to fix indecisive Navmesh Agent? 0 Answers
how to search for a random point until the condition is true? 0 Answers