So I am making an Asteroids Type Game, So the level is a single Square, and when the asteroid or player Exits one side the reappear on the other…
I am creating a Targeting Missile system. I have code that Targets Nearest Asteroids and launches missiles that track and follow them. But i want to be able to detect nearest asteroid on other side of border when ship is near it.
As an example (image Below)
Circle represents the current Closest Objects
Right now it detects a1 and a2 as closest.
but it should detect a3 also as closest
Here is the code that gets the objects and sorts them…
int SortDistance( Collider a, Collider b){
return (transform.position - a.transform.position).sqrMagnitude.CompareTo ((transform.position - b.transform.position).sqrMagnitude);
}
void ShootMissiles(){
Collider[] hitColliders = Physics.OverlapSphere(transform.position, 100);
System.Array.Sort(hitColliders,SortDistance);
//This is where we instantiate missiles based on distance
}