- Home /
Question by
Firedan1176 · Jul 04, 2014 at 01:54 PM ·
arrayenemydistanceforeach
I have an array of players. How can I get my enemy to go to the closest one?
I've tried this:
public GameObject[] enemies;
public Transform curTarget;
public float[] enemyDists;
public float curEnemyDist;
public int count = 1;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
enemies = GameObject.FindGameObjectsWithTag ("Player");
foreach (GameObject enemy in enemies) {
enemyDists[count - 1] = Vector3.Distance (transform.position, enemy.transform.position);
count +=1;
}
count = 1;
foreach (float dist in enemyDists) {
if(dist < curEnemyDist) {
Debug.Log("HELLO");
}
}
//This is constantly update the targetw ith the new/current target in this script..
GetComponent<AIFollow> ().target = curTarget;
curEnemyDist = Vector3.Distance (transform.position, curTarget.transform.position);
foreach (float dist in enemyDists) {
}
}
But it ain't working. I need to grab the transform of the gameobject in the array and get the distance between that and this.transform. How can I do that?
Comment
Already answered a lot of times. Just look for the logic in them and modify your code accordingly.
Your answer
Follow this Question
Related Questions
Check if specific object exist in list or array, Best Practices? 2 Answers
Attack selected Enemy Turn Based Combat 2 Answers
foreach a array inside a generic list 1 Answer
geting the closest object from a array 2 Answers
Change part of a string [Solved] 3 Answers