How to make my weapon attack the enemy nearest to it?

Hello, I’ve found this BP and implemented in my game. My gun has a sphere collision, and when an actor with the tag ‘enemy’ enters the sphere, it sets a reference to the enemy. I don’t really understand what happens when the enemy leaves the sphere thought.


Anyway, this doesn’t work very well with multiple enemies, the gun fires at the wrong locations. I wanted to do something like this, but make the gun shoot at the nearest enemy close to the gun. Anyone have ideas? Thanks.

Are your enemies actors or components? Where did you get this BP from? What kind of game is it that the gun needs an overlap sphere? Shouldn’t the gun spawn a projectile that has that sphere?

Enemies are actors. I took this from a Tower defense game. So you plant your gun on the ground and as enemies comes near the gun, the gun shoot at it. So if the enemy enters the sphere of the gun, it signals the gun to shoot. However, I’m not making a tower defense game, i’m making a game similar to vampire survivor / brotato, where you control a character holding a gun, and when the enemy gets close to the character/gun, its starting shooting.

Try Ticking something like this in the pawn BP:

  • a sphere overlap fetches nearby actors
  • its radius is the max range of weapons you’ve got atm
  • we sort by local_distances, here a local variable in a pure function
  • I draw a debug sphere at the closest target

Not optimal but could be a start. Instead of Tick, you could run this on a Timer, let’s say 10x per second. This can be further optimised by dynamically adjusting the size of the sphere overlap. If there’s an enemy close by, there’s no need to check for valid targets at the edge of the screen. Some other bits and bobs can be improved.

Would that work for you?

I using a blueprint where the character just shoot anything close to it. However, I cannot make the projectile work. Do you know how I can make a projectile shoot where the enemy is, given that I know his location? Also, it’s important that the projectile does not stop at where the enemy is, it should pierce the enemy and go through him.

edit: I tried using interp to with a timeline using set actor location, but does not work very well.

Standard projectile actor, overlapping instead of blocking:

Thanks this works! However my projectile is no a sphere, so its shooting sideways… My projectile is like a cone, and the tip should be facing the enemy.

The above already does it:

If you’re having issues, post:

  • the hierarchy of the projectile
  • the projectile’s setup
  • script that fires and orients it

I rotate the projectile 180 degrees and now it works. Thanks, much appreciated, this is the second time you saved me!

1 Like

In overlapped components, you chose ‘A enemy’. What if I have several enemies? It only accepts one class, not several. Is there a way that I can choose an actor that has a tag instead?

1 Like

You add them to an array.

Is there a way that I can choose an actor that has a tag instead?

Yes, query it for the tag.

It only accepts one class, not several.

Loop through classes. Or look into inheritance and target base class. Or use an interface. Or use tags.

I’m using a tag. All enemies have the tag ‘enemy’. The function ‘get overlapping actors’ doesn’t involve tags. Is there something like ‘get overlapping actors with tag’ or similar?

It returns an array:

It only works if I select ‘enemy’ as a class inside ‘get overlapping actors’. If I select ‘none’ it doesn’t work. Maybe this doesn’t work and I should look into ‘inheritance and target base class’ ?

Select Actor (base class of all actors) or Base class of the enemy if you’re using a custom inheritance chain.

1 Like

Ok, I selected ‘create child blueprint’ to make another enemy and it seems that it works now. Thanks!

Hello, first of all thank you a lot Everynone for your answers, it works well to detect the nearest enemy. I’m quite a newbie on unreal and trying to finish my first project. I have one question regarding your solution, my enemies are damaging the player whenever a collision box in front of them overlap the player. The issue is that it seems to consider that the detection sphere is a part of my player and so I take dmg when an enemy enters in this radius. Could you halp me with that please ?

Here’s my damaging event from bp_enemy