Prevent enemies' friendly fire in top-down shooter

Hi! I’ve been reluctant whether to post here or not since I initially thought it was a simple issue and could solve it without wasting someone’s time. However, I couldn’t find anyone who had the same or a similar problem and have been at it for a couple of days now (it’s for a jam).

So I’m making a top-down shooter consisting of only one room. There’s only 7 enemies, each a differentiated instance and their own separate object. The game looks like this:


(The “text” there is for debugging)

Right now, I have a “for each” event that picks the nearest object to the player whoose “awoken” variable is false, sets it to true, sets “look” to true, and plays a gun loading sound effect. This works something like an intro. The next event is that if “look” of enemies (a group containing all of the enemies) is true, then rotate towards player.X();player.Y().

The problem is that I want them to shoot, and I realized that, thanks to the room layout, there’s a big chance that they’ll shoot each other (in the back) if I just add simple shooting.

My first thought was to send a raycast from an enemy towards the player, test for enemies, and as an action (since the raycast hit something) maybe move the enemy away.
So I tried a couple of approaches, tried to implement AI chatbots to see if they could help me a bit, and lost days trying to get it to work. So I’m asking if anyone has any idea of how to make this behaviour come true. Thank you so much for reading!

Here are some code snippets:


(“John_Nato” is the name of the enemy I’ve been testing with. I think I’m going to have to repeat the event/s 7 times (for each enemy), but if you have any idea how to avoid that I’d be grateful if you’d share it below. I’m way past efficiency.)


(Last approach of tweaking the raycast angle, like a blind person with a stick on the street, if you will)

Ok seems like I got the raycast part working…


…judging by what the text is printing (goes blank with no collisions and displays the X of the collided object when there is one).

I need to find a way to move the enemy away from the collided object. I thought maybe this way:


Where the bottom sphere would be the player, the top one would be the enemy and the middle one the obstacle. I still have no idea how to implement this. What do you think?

Okay but what’s the game about? I just don’t get what should happen and when.
Also, instead of a text obj, you can use the built in debugger which is pretty handy.
:slight_smile:

Well the game is about shooting, and about avoiding being shot. Thanks for the debugging suggestion!

Apparently while the raycasting and the selecting the closest enemy obstacle work, I apparently can’t move the enemy in any way shape or form. Even though he doesn’t have te Physics behaviour, forces don’t seem to affect him.