Choose Closest Point in array

Hi there thanks for looking. The problem i am having is i need to be able to seleect the three closest spawn points held within my array. I need to pass in the closest and remove the furthest away but only allow 3 to be possible spawn points.

/* 
Here there will be the functions that will deal with every aspect of the games enemy spawn system
There will be functions that are not required but i am testing as much as possible for performance
So when the game gets closer to be playebale we will remove the uneeded functions for performance wise
*/
var spawnPoint : Transform[];
var basicFodder : GameObject[];
var currentEnemies : int = 0;
var maxEnemies : int = 12;
var minEnemies : int = 8;
var spawn = true;
var bBasicFodder : boolean = true;
var spawnWait : float;
var spawnTriggers : Transform[];
var currentSpawn;
var minDistance : float;
var points;

function Update()
{
	for(i=0; i<spawnPoint.Length; i++)// Checks the dist between all spawns and player
		{

			var distance = Vector3.Distance(transform.position, spawnPoint*.transform.position);// dustance for all elements in the array*
  •  	if(distance < minDistance)*
    
  •  		{*
    
  •  			print("It Works");// Tell us it work*
    
  •  			minDistance = distance;*
    
  •  			currentSpawn = i;*
    
  •  			BasicFodder();*
    
  •  		}*
    
  •  }*
    

}
*/// our basic fodder complete with spawning \*
function BasicFodder()
{

  • while (bBasicFodder) // While bBasicFodder is true run the statement*
  • {*
  •  //if(bBasicFodder == true) // if our basic fodder is true start creating enemies.*
    
  •  //{*
    
  •      if (currentEnemies < minEnemies)* 
    
  •          spawn = true; // if minEnemies reached start spawning*
    
  •      if (currentEnemies >= maxEnemies)* 
    
  •          spawn = false; // if maxEnemies reached stop spawning*
    
  •      if (spawn)* 
    
  •      { 	// if spawning enabled create new enemy*
    
  •          // Randomize the different enemies to instantiate.*
    
  •          var m_Object : GameObject = basicFodder[Random.Range(0, basicFodder.length)];*
    
  •          // Randomize the spawnPoints to instantiate enemy at next.* 
    
  •          var m_Position : Transform = spawnPoint[currentSpawn.length];* 
    
  •          Instantiate(m_Object, m_Position.position, m_Position.rotation);* 
    
  •          currentEnemies +=1;*
    
  •      }*
    
  •   //}*
    

yield WaitForSeconds(spawnWait); // free Unity for 2 seconds each loop anyway

  • }*
    }
    Now the for loop only iterates the length of my array i need it to constantly go through and pick the closest 3 points on which to spawn from. Note this is not a zombie game!
    So in short i need help picking the 3 closest points to me then pass those values into the BasicFodder Function so that it will only spawn from the 3 points that have been chosen and closest to me. Thanks For Looking And i hope you can help me.

First of all I think you want to kick off your BasicFodder Coroutine in Start() or something similar instead of update and start it using the MonoBehaviour.StartCoroutine method. Example here.

Also in your update method you obviously need to find the top 3 closest points instead of 1. I would do this by making a new data structure that pairs an index and a distance, then I would add calculate all the distances (it would probably be good if you could add some kind of system to crop out positions that are very far away so you don’t have to do calculations on them depending on how many spawn points you have) and add the pairs to a list and sort it. This is probably easier done in C#/.net I don’t know anything about doing that sort of thing in Javascript. It would be best to do these calculations in a repeatedly invoked method or even in the BasicFodder method because it doesn’t need to happen every frame. Then choose your spawn point based on the index value of one of the top 3 items in that list.

In your code right now you use the spawn point at index “currentSpawn.length”, I don’t even know how this works in Javascript since you assign currentSpawn as an integer earlier, I would imagine this should just be currentSpawn if you want it to spawn at the closest spawn point right now.

To recap:

  1. Start your coroutine from a coroutine friendly method that is not Update using MonoBehaviour.StartCoroutine()
  2. Compile a list of all the distances to your spawn points and sort the list based on distance choosing one of the top 3 after the sort as the index you’ll spawn at.
  3. Change “currentSpawn.length” when getting m_position in BasicFodder to “currentSpawn” for the time being to at least get it to spawn the at the closest point.