collision detection – What would be the best way to find a Game Object that is closest to another Game Object in unity?


I made a 2d space shooter game awhile ago and I want to go back into it and overhaul some things. I’ll provide a link to the game so anyone can play and get a better understanding of what I’m trying to do.

https://your-pal-drewdle.itch.io/starspeed

With how the game is currently set up, all the enemies are constantly setting their rotation to be facing the position of the player. I want to go back in and add a co-op mode where there are 2 players, however, if I just added a second player without changing any code, the enemies would always be facing Player 1, never directly facing Player 2.

I was thinking that I could get around this by giving each enemy an invisible circle with the center at the enemies position that would constantly expand until it hit one of the two players, I could then set the enemies rotation to face the point where the circle collides with one of the players.

In theory, this would make it so that each enemy is facing towards whichever player is closest to it.

So my question is: How could I go about implementing this in unity? Or, is there a better or more efficient way to do this that would yield the same result? I’ve spent a decent amount of time researching this and I haven’t had much luck.

All help is appreciated, if you need more explanation just let me know and I will do my best to explain in more detail.