Distance of OffScreen objects..

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
}

might help :slight_smile:

ghost ships section at the bottom looks promising… essentially create off screen “fakes” of the ones you can see

Thanks for the link, that is really interesting read and gave me ideas for some other features i want to use.
it did give me an idea, what if i create a 4 more overlapsheres to detect the Offscreen Objects… Im going to try it out and let you know how it turns out.

So im working on coding the 4 overlapsheres and i decided to use a layermask on them to filter out other objects.
but now its not detecting Anything. As seen when i try to Print the hitColliders[0]'s name.
here is what i changed. The Asteroid Prefabs have a Layer called Asteroid.
am i using the layermask wrong?

Collider[] hitColliders = Physics.OverlapSphere(transform.position, 100,LayerMask.NameToLayer("Asteroid"));