- Home /
Get list of nearby targets (JS)
Hello,
I've been attempting to make a function that allocates all nearby gameobjects into a variable and then sorting them by range, but haven't had much success. Would a list or array be more appropriate and how would I be able to implement it successfully?
Answer by sfc.itzhak · Jul 28, 2012 at 10:45 PM
you have 2 ways:
put a trigger with a colider in the center of your GO and you can check what hit the colider if you are using JS
make an GO array
function OnCollisionEnter(collision : Collision) {
array.Add(collision .gameobject);
}
if you are using c#
http://msdn.microsoft.com/en-us/library/system.collections.arraylist.add(v=vs.80).aspx
and for the sorting use this example
http://answers.unity3d.com/questions/22261/sorting-builtin-arrays.html
get all the game objets in the scene and filter it first by the minimum distance you want distance and then by the distance itself.
filter any Go outside your radius
var allgameObjs : GameObject[] = FindObjectsOfType(GameObject) as GameObject[];
var gameObjs : GameObject[];
for(i:int=0;i radius) {
gameObjs .Add(allgameObjs[i]);
}
}
then sort them like here
http://answers.unity3d.com/questions/22261/sorting-builtin-arrays.html
this sould give you an idea.
i wrote this in the web so syntex might be wrong.
sfc