AI find nearest enemy

Hi all.

I’m working on a team based combat project and I’d like to know how to have ai bots focus on the nearest enemy. I can’t use “all actors of class” because I have several different actors (troop types) per team. I’ve tried “all actors with interface” but don’t know the proper node system to set up.

(I’m using blueprints and behavior trees.)

What’s the condition that makes another bot an enemy? TeamID?

At this moment the bots have trigger spheres in their pawn blueprint. On event overlap they check each pawn for an enemy tag. The problem is that when two enemies overlap at the same time one goes undetected.

I’ve never heard of TeamID, is that possiblly what I should do?

then you want to get the closest enemy by checking distance and tag , yes?

Yes. I think so.

Here is a solution


That sounds like a task for EQS.

I thought so too but EQS can only target actor classes, I need something that targets multiple actor classes.

I’m afraid this didn’t work for me. I can’t use “all actors of class” for my project. I need something else.

If you make a parent that all of your actors share, you can “Get All Actors of Class” and search for the parent, it will return any actor that has that parent. No matter how far down the hierarchy.

I’m trying to make my enemy destroy multiple actors of the same class. However, he only destroys the original and won’t destroy the copies. I tried this method and he stopped after he destroyed the parent. Would you know of how to use your method for my situation or do you know of a better method?

Are you running the EQS again after the first one is destroyed?

Are you copying in game or have you duplicated in the content editor? If you duplicated his file outside of gameplay, a better way to do it is right click on the actor blueprint and create a “child blueprint class.” I think that maybe, in your case, the actors might not be properly parented. This should parent them.

hope that helps.

Fantasifall: I was finally able to get to it, and it worked perfectly. Thanks.

Not yet. For now the enemy runs EQS on the human player. The human player has minions on his side but I haven’t yet made a context for them. I want the enemy to attack all the minions first and then attack all the player. Which, the enemy does do, however, he only stops after the first one.

Also, when I look at the debug while in game, Minion, the blackboard key that is a reference to the actual minion, is set to none after the first one is destroyed. Do you think EQS will help with this?

I alt-clicked the first minion and dragged out to make a duplicate. I haven’t tried duplicating in the actual content browser. I’ll try what you said. Thanks for the help. I eventually want to be able to spawn them in waves though.

Edit: Unfortunately, this didn’t work. He still stopped after the parent was destroyed.

You have to get the next minion once the one you are attacking has died. If the blackboard is none after the first one is destroyed, then you need to tell it to find a new minion to attack.

Say you run the EQS test on the human player, then run another EQS to find a minion - if that returns with nothing, then you can attack the player. If it returns with a new minion, then attack the minion. Things get a bit tricky if there are friendly minions and you’re using blueprints, but we’ll cross that when we get to it.

I’ll try that. Thanks for your help. I haven’t done anything with C++ yet. Do you recommend I use C++ for this complex of AI. I’ve only been using the engine since February and have only worked in blueprints.

You’re welcome.

C++ vs Blueprints is a difficult question. It’s really a matter of what works best for you and it’s not an all or nothing decision. Blueprints are great for rapid prototyping and allowing non-programmers to create. As a programmer I find they quickly get difficult to follow, encourages really bad habits and you’re limited to what Epic has exposed for you. There are also some major issues with them when working on a team using source control.

I would say that if blueprints are working for you, then there’s no reason to switch over yet. There are a few AI features that aren’t available to blueprints - such as making your own EQS tests - that you can always try using C++ for without having to move everything else over.

Alright. Another thing: How do I have the EQS, the behavior tree, and the controller work together? I know how to run it in the behavior tree but I don’t know how to tell it what to do after it finds something.