- Home /
Simple question about targetting
okay so I created a targeting script but I had some problems
so I posted my question.
http://answers.unity3d.com/questions/184053/targeting-multiple-enemys.html
the answer that I got was
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
There are a couple of things here- you'd need to restructure the way the enemy finder works, since at the moment it can only return a single enemy! You first need to make it return an array or a list (preforably a List, for simplicity's sake), then populate the list in the function.
First up, put 'using System.Collections.Generic' at the top, so that you have generics (which are really useful).
public IList<Transform> FindEnemiesInSphere(float radius){
Collider[] cols = Physics.OverlapSphere(transform.position, 10);
SortedList<float, Transform> transforms = new SortedList<float, Transform>();
foreach (Collider hit in cols) {
if (hit && hit.tag == "Enemy"){
float Dist = Vector3.Distance(transform.position, hit.transform.position);
transforms.Add(dist, hit.transform);
}
}
return transforms.Values;
}
This will give you a list of Transforms which you can iterate through with a Foreach, and which will be sorted in order of distance!
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
MY QUESTION IS how do I iterate through with a Foreach? So, that I can select the targets with tab