Ok so im trying to set up my turrets to fire at the “First” guy. so i made this script to shoot the farthest guy in the lane, that is within the range of the turret. im stumped… it looks like it should work to me. but in game the targeting is all around the map, he sometimes shoots the first guy, but mostly his targeting is just unpredictable.
can anyone see whats going wrong?
or if you have a better way of aiming id love to hear it.
( fyi the enemies are given a number as they spawn. first enemy has ID of 0 and the last guy in the wave (#20) has a ID of 19.)
function reAim()
{
smallestNum = 100; //if it was at 0 he wouldnt aim at anyone
for(var target: GameObject in Spawner.enemyRanks) //for each guy in the enemy array
{
if(Vector3.Distance(Tilt_Aimer.position, target.transform.position) < range) // if this particular guy is in range
{
targetNum = target.GetComponent(Evil_Cube).getID(); //get his wave ID
if(targetNum < smallestNum)//If its smaller than the other IDs this turret has been though
{
targetNum = smallestNum; //set the smallest encountered ID to his
myTarget = target; //and aim at him
}
}
}
}