I’ve started working on a top down shooter game, which is going to have several weapons to pick from during combat. I’ve got my pistol to feel pretty good, and my shotgun is too. The only problem is, I think I can make it run more efficiently.
This is the shotgun in action. It runs good, but initially had a little trouble creating the three bullets and then having them all fly different directions. I just wanted to get it working, so to make it work, I just made two more bullets.
I figured that this should work fine for the time being, but now that I’m focusing more on combat, I want to add two more bullets to add a better spread to it. To add on top of this, I have to write code for collision with all of these extra bullets as well. Is there a way to create bullets like how I’ve done for my shotgun, but only use one bullet instead of three different instances? My code is below. Weapon one is my pistol, weapon two is my shotgun.
You only need one bullet object for your shotgun. In the part where you create the 3 bullets, loop 3 times creating a bullet and adding the force and angles to each newly created bullet. Something like:
Change the value of _counter and the angles accordingly to accommodate more bullets.
Thanks! This works wonderfully. There’s a small problem though. The spread on the shotgun works wonderfully, but now whenever I shoot an enemy NPC, the entire spread of bullets only counts as one bullet hitting, so even if all five bullets hit, the enemy only loses 1 hp. Is there a way to fix this so that the enemy registers each bullet as a hit? Here’s the enemy and bullet collision code at the moment, though there isn’t much to it.
Try using a foreach event, and iterating through each bullet and checking whether it collides with the NPC.
This isn’t the most efficient solution, but because of the low number of objects, it shouldn’t impact performance.
Works perfectly now! Thanks for the help!