- Home /
Physics.OverlapSphere to get 5 Nearest objects
Ok, So I'm making a Space RPG like game, and I'm having a bit of trouble with the targeting systems. I want the 5 nearest objects within a certain distance to be selected to be targeted. Here is my code for this, but it doesn't work. What am I doing wrong?
//VARIABLES
var targets = new Array();
var selected = Physics.OverlapSphere (transform.position, 500);
var i : int = 0;
function Update () {
for(var n = 0; n < selected.length; n++) {
if(selected[n].gameObject.name == "Enemy"){
targets[i] = selected[n];
i++;
}
}
for(var p = 0; p < 2; p++) {
///Target[#] Events go here
}
}
Comment
Your answer
![](https://koobas.hobune.stream/wayback/20220613061726im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Getting vectors on an object every so many units 0 Answers
object distance 1 Answer
find closest target? 1 Answer
Calculate distance between AR object and camera 1 Answer
How to do something when an object is in the player sights. 2 Answers