Hello guys,
I know this question has been asked, but no solution works for me so far.
Here is what I got:
I want a AI Unit seek for the closest Enemy, walking towards and killing it.
I used the example Script from the Unity Documentation, which works so far.
But when the Unit kills the 1st Enemy it wont refresh the closest target value.
Here is my code:
var targetEnemy : GameObject[];
var closest : GameObject;
var distance = Mathf.Infinity;
var base : GameObject;
function Update () {
findEnemy();
if (closest != null){
kill();
}
}
function findEnemy () : GameObject {
targetEnemy = GameObject.FindGameObjectsWithTag("Enemy");
for (var go : GameObject in targetEnemy) {
var diff = (go.transform.position - transform.position);
var curDistance = diff.sqrMagnitude;
if (curDistance < distance) {
closest = go;
distance = curDistance;
}
}
return closest;
}
function kill (){
GetComponent(NavMeshAgent).destination = closest.transform.position;
}
Hope you guys can help me once more and thank you in advanced.
Greetings,
Nitrosocke