Trying to implement a mechanic so only 3 enemies at a time get into melee with a player.

So I haven’t been able to find a solution for this as I’ve been looking around online but couldn’t see any relevant topics, but I’m trying to make a melee combat game, but in particular since there’ll be quite a number of enemies, I want to make it so only 3 enemies at a time run toward the player to fight them in melee, and all other enemies hang back moving around from a distance while focused on the player.

I’ve tried a few methods, such as adding a collision sphere so they couldn’t approach, but that didn’t work very well as it could easily break if the player was closer to a ‘body’ on the ground than a living enemy.

So, just wondering, is there any particular way to get the enemy AI to stay a bit back from the player while still moving around in a radius close to them to let a few fight, and once one goes down, a new one moves in?

In particular, the best way of handling this on my end would be through the enemy AI controller/behaviour tree.

I have not done this so the following idea may be bad…

  • you say you want this handled by the AI… …what if the AI had a collision box that was checking for your player. Also, the AI’s have a dispatcher method for communication to a decision making actor. The decision making actor is listening to those dispatcher messages, tracking how many enemies can melee and then communicating to all AI’s who can melee the player. If the AI is dead, it would not send a dispatcher message when its collision box triggers. The AI’s, when their collision box is triggered, would stop, send their dispatching message, wait some time for a possible message from the decision making actor and then either hover around the player or move in to melee.

Thoughts???

If the AI dies, or flees, the AI needs to send a dispatcher message to the decision making actor that it no longer should be considered as a melee player such that someone else can go in an melee. This implies that a hovering AI is still listening to the decision making actor for its chance to melee the player.

It’s pretty simple of you bust it down to it’s base, IMO.

You need a central authority that keeps track of how many NPCs are attacking the player. Probably easiest to actually put this in the Player. When a NPC goes to attack the player, increase the count by one. When a NPC stops attacking the player, decrease the count by one. Have a “Can I Attack This Player” function, which returns a true if it’s ok, and a false if not.

In your AI Tree, have a service that monitors whether attacking is available or not.

You may need to keep track of which NPCs are attacking, not just a count of them, depending on how you need to use the information.

2 Likes