0
\$\begingroup\$

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.

\$\endgroup\$
3
  • \$\begingroup\$ What have you researched so far? There plenty of examples online. \$\endgroup\$ – Saad Anees Sep 13 '20 at 6:29
  • \$\begingroup\$ Why so complex? Just check the distance to all the players and pick the closest one. As Saad Anees said, you can easily google how to get the distance between 2 transforms in Unity. \$\endgroup\$ – Nikaas Sep 13 '20 at 7:39
  • \$\begingroup\$ Admittedly I've only researched how I could implement my solution, I thought I could use circle casting but I found that it doesn't work the way I thought, someone explained it as it's like throwing a frisbee. Nikaas, I'm sure your solution would work, thank you for your reply, but I have another question. I was planning to add the same expanding circle idea to the player's rockets and have them home in on the closest enemy. Would your solution still work? Would it be possible to make a list of enemies that are on-screen and compare the positions of each enemy to the position of the rocket? \$\endgroup\$ – Your Pal Drewdle Sep 14 '20 at 17:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.