UE4.27 - Picking up various objects from world and firing them as projectiles all at once

Hello! I am an amateur developer working on a game where you will pick up specific items from killed enemies and use those as ammunition in a shotgun-spread.

It’s been okay so far, but I’ve run into a few small snags working on this prototype. The items are actors in-world, and are added to the inventory array when you walk over them. When you fire, it reads the inventory array and spawns the respective actors as projectiles with velocity, adding a projectile movement component, tag, and setting their Is Projectile boolean.

I considered making a corresponding Projectile actor for each kind of loot, but figured it would be a waste if I could figure out how to convert the pick-up actor into a projectile actor.

The problems in question are…

  1. When spawned, the loot actors collide with one another. I had previously fixed this with the default FirstPersonProjectile when figuring out the spread, but I cannot remember what I did to fix it. Their collision presets are set to overlap Projectiles, which I have tried setting them to in order to fix this, but to no avail.

  2. The projectiles have no velocity. I suspect it’s because the projectile movement component is being added after the fact, or a transform is not being applied right, but I am not sure. They also do not despawn after a certain period of time as intended.

  3. This is an array issue, but when picking up multiple of the same item type, they appear to “overlap” in the array. Picking up 3-4 items will only fire two, two will fire one, etc.

I can provide screenshots of my blueprint code. I am a BIG novice in regards to all this, so forgive me if my code is inefficient!

Hey @Bugbuh! Super interesting concept!

I have some ideas to fix a lot of this very easily, however I am going to definitely need snips of your code!

#3 especially should be super easy. :slight_smile:

Thank you so much!

Here is a few code bits, for shooting and loot pickup. This is all in the FirstPersonCharacter BP!

So! For the speed, drag off of the return value on the “Add Projectile Movement Component” and SET initial speed and max speed to whatever value (you may need to add impulse as well).

You should probably set the projectiles to ignore projectile channel for collision if you plan on firing these with any sort of speed, or they’ll collide with each other and that’s a bad time.

And lastly, for the overlapping multiple actors of same class, are you sure you are firing only one? Try setting up a printstring after the spawn on shoot so you can count them when you fire- they might just be overlapping down to the pixel!

Understood! I’ll give this a try.

As for the overlapping multiples, I am pretty sure. Since they’re colliding with each other, I’d imagine if they were overlapping they’d explode outwards, but after some fiddling with the spawn actor’s collision options, I had set it to where they’d simply fall to the ground in a spread. There were definitely only one or two projectiles despite being loaded up with up to five!

I’ll give the printstring a try and report back on how these solutions go. Thank you!

Okay! Tested these out, got some mixed results.

They are no longer colliding with each other, but still do not have any velocity after setting initial and max speed.

As well, I discovered that it is actually only spawning one or two at a time, but the others remain in the array. This is not as intended, as it should be spawning all the items from your inventory at once.

Here is the velocity code that didn’t work.

All right now, you’re gonna have to do some cleanup if we want to continue here :joy: having a hard time squinting to read! Don’t be afraid to spread it out- readable code often takes more than one screenshot. :slight_smile: But yeah can’t add impulse to something that isn’t simulating physics, try putting the impulse AFTER the set simulate to true. Also I think you’ll want velocity change TRUE on that node, but try it both ways.

Regardless, adding impulse doesn’t really help with a projectile, that’s what the projectile movement component is for, though typically the initial speed is HOW you get it to fire…

I was indeed worried that setting initial speed after adding the projectile movement component wouldn’t work as it’s being set AFTER spawn, not on spawn.

I take it “Sphere” is the ROOT of the LootBase?

Do you want the loot to be re-pick-up-able afterwards? Because then we’re not looking at projectiles, we’re looking at objects to be manipulated through physics.

Sorry about that! I’ve tidied it up a bit, hopefully these are more readable!

Sphere is indeed the root, though it’s the collision sphere. Using the actual sphere mesh (which is currently being used as a placeholder) doesn’t work either.

As for being picked up afterwards, not quite. Really the intent is for it to be destroyed a bit after impact, but none of the code to DestroyActor works for some reason.

From the start, here’s the code to shoot, now with the more cleaned-up speed settings. Putting SetImpulse after the SetSimulate also didn’t work, unfortunately.


Here’s the projectile code.


So one thing I noticed just gleaning over, and then a suggestion for testing.

This:


This is getting the target’s velocity, multiplying it by 100, and then granting that impulse. However… Your velocity is 0. So yeah… gotta add haha.

And when granting impulse you need to use biiiiiig numbers. In the thousands, usually.

The suggestion is, you need to add printstrings all throughout this to see what’s stopping execution. Because if the destroy actor nodes aren’t working, that means there’s an issue somewhere earlier in the string of execution. The printstring notices will tell you where in the string it’s breaking, judging by what the first string NOT printed is. :slight_smile:

Right! That code was brought over from the (working) projectile BP i had prior used for testing. Multiplying the velocity and adding it was meant to add impulse to what it hits. Hopefully, figuring out the velocity will get it working.

I’ll try adding more printstrings! Right now my priority is getting the LootBase actor to fire properly as a projectile. It is unfortunately still having the issue where it doesn’t fire every projectile at once, only one to two at a time.

Considering just making a corresponding projectile actor BP for the loot pickups at the moment until I can get this figured out! There’s nothing that seems like it shouldn’t work, but it’s just not.

Well why don’t you try this:

Make a projectile class.

Then, on shoot, ForEach member of your array of picked up loot,
Spawn projectile of that class, set static mesh to that index’s “LootBase” object’s static mesh and the collision volume to be the same dimensions as the loot picked up?

ALSO now that I think about it for THIS- this may be the issue. Are you spawning the objects far enough away from the player that they aren’t hitting the player’s collision?

Ohh, interesting solution! Setting the static mesh is a good idea. I have set it up to where a picked-up loot item corresponds to a fired projectile and it works! It seems the problem was adding the projectile movement component after the fact.

Right now, the only remaining issue is the weird “array overlap” where if you pick up 5 pieces, it shoots 3, then 1, then 1 instead of all 5 at once.

Hmm. There have to be more clues we can get. Are you using 5 unique objects?

Try putting a printstring on the ForEach loop, right after the loop link the element to the print string so it grabs the name of the object and prints it. Maybe there’s a noticeable pattern there.

Also you can try putting a delay right after the ForEach and setting it to .05 or something. It may have something to do with overloading one tick.

The objects are each of the same class, placed in-world as actors.

The Print String didn’t really reveal anything new; It just printed the three objects that shot, then one, then one again.

The delay actually seems to have changed stuff, but not necessarily for the better. Now instead of shooting three projectiles, it shoots one per fire input.

Hmm. Okay let’s take the delay out then.

So to be clear, if you hold the fire button down, it fires 3 objects with 5 remaining.
It doesn’t fire again until you release then press again,
then it fires one with two remaining,
then it fires one with one remaining,
and then the LootAmmo array is empty and no longer fires. (please clarify if this ISN’T what is happening)

Would you mind posting the fire code as it is now (without the delay node)?

That is correct!


Here is the relevant bit of the fire code. It reads the LootAmmo array and for each actor in it checks that actor’s class against a corresponding projectile via the Check Ammo function, then spawns the projectile and removes the actor from the array.

I’ve got it!

It’s removing the parts of the array as it moves through, meaning if you have 5:
Shot->FE->index 0 → Set Array length 4
FE->index 1 → Set Array length 3
FE->index 2 → Set Array length 2
FE->index 3 → INDEX 3 INVALID as index runs 0-1.

Shot->FE->index 0 → Set Array Length 1
FE->index 1 - INDEX 1 INVALID as Index is only index 0

Shot ->FE->index 0 → Set Array Length 0

Replace the ForEach with REVERSEForEach, making it start at the highest index and work down!

That fixed it! Thank you so much!

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.