var OnSightTowers = Collider[];
function Update(){
LookingForTarget();
}
function LookingForTarget(){
onSightTowers =Physics.OverlapSphere(transform.position,sightDistance,TowerMask);
if(OnSightTowers.length!=0){
myTarget = onSightTowers[onSightTowers.length -1].transform;
}
}
Hi all,
I have a little bit problem here. Above is my code. I’;m planning to use OverlapSphere as my AI sight area.
The problem is the content of onSightTowers Array keep changing. Whenever new target get closer, The array order is changing. Some time an object is inside array[0], but the next moment it become arrya[1],So I have no idea how to find the closest target. The closest target is the first target that collide with the OverlapSphere.
Sorry for my bad English. Do I explain it clear enough?
You need to find the closest of the results yourself:
function ClosestToPoint( point : Vector3, list : Transform[] ) : int {
var distance : float = list[0];
var closest : int = 0;
for ( var index : int = 1; index < list.length; index++ ) {
if ( (list[index].position - point).sqrMagnitude < distance ) {
closest = index;
distance = (list[index].position-point).sqrMagnitude;
}
}
return closest;
}