Spawn bullet from character which auto target closest enemy in range.

Hi all,

After years and years I am finally working on a wave survival game.

Am a bit older and not that good at Unreal yet but giving it a go.

Question:

How can I use Blueprints to let a projectile spawn from the character itself (so not a gun) without an input action and have it auto target the closest enemy actor in range? Preferably it should shoot like 5 times and have a cooldown of a few seconds and fire again. (I will work on upgrades which increase bullet speed, increased shots etc at a later stage).

Edit: I know how to create the “spawn projectile” and place it on the caracter just the other parts.

Thank you in advance,

Wrench

There’s a Find Nearest Actor node you can use for that:

If it returns a target, use find lookat rotation between shooter and target, and wire than into the spawn transform of projectile.

Thank you for your answer.

While it sure helps to know about the Find Nearest Actor, I did not get it to work cause well I guess I am not knowledgable enough to apply this and the rest of my question. Seems my projectile also does not work. So going back to the drawing board.

Kind regards,

Wrench

Find Nearest Actor is a bit finnicky since it needs to know which actors to check. If you add a tag to anything that you want affected, you can use get actors with tag and feed that array to it:

Teh spawn loc can be whatever, I jsut used the muzzle socket on the grenade launcher glued to my test turret.

I called the function in a looping timer like so:

So the turrent spams green balls at me every .5 seconds. Should have probabky set a target actor var and rinterped towards it since the hard rotation set in function is kinda snappy. A real AI pawn with focus set would rotate more smoothly on its own tho.

Thank you for the comprehensive answer I will give it a go and revert back here!

Heya again,

So I got it working as an autofire to closest actor with a tag…., However the issue now is that my character kept turnng around and looking at the closest enemy actor while doing the auto fire. (Which really makes for choppy gameplay)

I need it to keep autofiring the closest actor tag I assigned regardless of which way I am facing :smiley:

I am now trying to fix this and puzzling on how to do this.

If using the bowl of code noodles I posted, just delete the set rotation node.. Projectiles might try to fly back through you though. Could always use homing settings in projectile movement component, and pass the target through to the bullets when they spawn.

Yeah that stopped the choppy movement parts. Great stuff! Just wondering why the bullets are not auto switching to the nearest actor, it keeps shooting at the first one. And yes I did use your BP example and implemented it with what I had already and am trying to understand the logic of why this works.

Edit: Don’t know what I did but it works! Thank you so much. The targeting is not completely straight as it seems the bullets are shooting to the right side of the enemy char.

I can move on to the next stage and have my bullets actually do damage and enemy characters take damage.

As for bullets not locking on, are you calling the autotarget func for every shot? Or do use a separate lockon function triggered by a key press? If using a dedicated target lock func, you’ll probably want to make an actor type variable from the return of find nearest actor ( just right click teh blue pin on it and promote to var). Then use that actor reference for the targeting maths. As for missing the target, if enemy is moving, the projectile is going to be aimed where target was when fired. When setting up the lookatrotation, you could do something like target postion + its forward vector * speed to aim a bit in front of them. Or, depending on the design of the game, you could use an AI pawn which follows the player around and shoots thing. Like the pods in Nier Automata. AI controllers can use perception system and its prediction sense to hit moving targets. In theory, you might be able to do that on a player too. I’ve never tried that so not surre where to start.

Here’s a tut showing how to set up AI prediction tho:

Base Criteria

  1. Get a list of all Possible targets in range.
    Multi-Sphere Trace for Objects (custom capsule component)

  2. Loop targets discarding any that are occluded.
    Multi-trace each potential target to determine viable exposure.

  3. From set, find nearest target

  4. Determine where to shoot at… Select a non-occluded bone.

  5. Calculate Aim velocity for projectile. Spawnlocation, Target Bone location → Suggest Projectile Velocity

  6. Spawn the projectile, passing Aim velocity (exposed on spawn, Instance editable) and setting it PMC velocity in construction script.

  7. Use a timer for RoF and Shots fired/Max shots conditional.

  8. Clear target reference once Max shots has been reached.

  9. Cool down for duration

  10. Find new target.

A lot of info there so let me reiterate:

The bullets are locking on to the closest target and mostly hit.

Just visually it is not at the center of the enemy. Not sure it is because enemies are moving or if my visual targeting is off.

If the target is moving you need to predict its forward location based on movement direction and velocity. Also have to consider the projectiles velocity.

Basically you have to shoot ahead of the target and let the target run into the projectile.

1 Like

HI again lads,

Thank you for the help so far!

I managed the following:

A playable actor to move around

A projectile

Random spawning enemies

The playable actor to autofire on nearest random enemy.

A working healthbar(!)

Player can take damage (I used a PainCausingVolume for testing here… I do not have damage on my projectile or on random enemies taking damage)

A working respawner on a 5 second timer

The issue I am currently tackling: How to ensure the player character to cancel the auto fire when he dies and restart it when he respawns?

Thanks again!

Kind regards,

Wrench

Edit: I figured it out: I use: Bolean → Not → Branch and it seems to work :smiley:

Hi again,

All the things in the above post are working except 1 issue that is now occuring:

My Autofire shoots every closest target in range however:

When I move forward and the target is behind me it seems the Bullet is firing through my character which causes it to have a collision event and makes the target jerk/move backwards.

This only happens when the character is moving not when it stand still and fires backwards.

Solutions I tried to apply:

I tried making an exception called friendly fire in the File/Projectsettings/collision ticked it on ignore but it did not solve the issue.

Then I tried to add after event begin play the ignore actor when moving (capsule component) and actor as self.

Unfortunately, I think I might be doing something wrong as it it not solving the issue.

And I do not want to mess up my code so far with trystorming as it was hard enough to get to this point with my limited knowledge.

I did get it to work by putting the projectile on no collision but then it does not hit my enemy actors as well.

Any input would be greatly appreciated.

Kind regards,

Wrench

Hi all,

I kinda solved it by actually adjusting the capsule component by making the height smaller and placing the ProjectileSPawnLocation above it.

Probably not the best solution but it is a workaround.

Kind regards,

Wrench

To skip actors behind the shooter, promote the result from get actors by tag to a local variable, and do a dot product test to see if they’re in front of the user. and remove the actor from the array if its not.

I added a sequence at the start of my autotarget function. do this first, then the wire going offscreen at bottom of pic goes to the get nearest actor and spawn projectile stuff. Could just run that stuff from the loop complete pin tho.

Proper solution would be to have the projectile spawn on the correct side.
Add 4 sockets to the turret mesh, one on each side.
write a little function to get the closest socket to the target.
Use that socket for projectile logic and spawn location.