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.
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.
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:
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.
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
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:
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.
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.
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.