So let’s talk about weapons. I’m about to start coding up my basic weapon classes, so I thought you might like to know a little about what makes them all different. I mean, that probably seems obvious, but I’d never really thought about it before now so why not.
Here’s how the setup works: I have two “base” weapon blueprints; one for the third person weapons and one for first. Both of these blueprints are simple. They have no meshes (physical bodies), and only a few variables that all weapons will need in order for the inventory system to use them. These blueprints also control the base visibility options, since I need third-person weapons to always be visible to other players, but to disappear for the player when they are in first-person mode. Similarly, I need the first-person weapons to be hidden from everyone else, including the player, unless they are in first-person mode.
From there, I have seven weapons: pistol, shotgun, assault rifle, sniper rifle, grenade launcher, rocket launcher, and knife. All other weapons are going to be a derivative of one of these.
Each of these weapons has two blueprints, one is a child of the third-person base, the other the first person. It therefore inherits all the inventory variables (which I then change for each weapon) and its visibility settings. The third-person model will also eventually have complete firing behavior added to it, while the first-person one will only have muzzle flash behavior added so you know you fired it.
From there, we build the weapon’s specific behavior. Just as how each of these weapons is a child of their respective base weapon blueprint, you create specific weapons by creating child blueprints of these weapons. That way, every time I want to add a new weapon, I don’t have to completely recode the behavior each time. I can just make a child of the basic weapon and then change the variables like magazine size, base ammo, firing rate, reload time, all that crap.
So what one needs to know from there is what kind of behavior they want. Here’s how it breaks down.
Pistol: a hit-scan weapon, doesn’t actually fire a projectile but a raycast. Basically a straight, invisible line that returns whether it hit anything of interest or not. Maybe it places a bullet hole decal at its collision point if it doesn’t hit “flesh”.
Shotgun: kinda like if you tied several pistols together. It fires several raycasts in a spread.
Assault Rifle: fires a single raycast at a time, but can fire them rapidly in succession. The crosshair should move a little bit each time the longer the player holds the firing button, to simulate a loss of accuracy over time.
Sniper Rifle: single raycast, but fancier scoping abilities.
Grenade Launcher: this is were it gets interesting. The grenade launcher will fire a physical projectile at an arc rather than a straight line. This means that weapons you might not expect would be based on the grenade launcher. Shit like crossbows or, say, the Team Fortress 2 Medic’s flechette launchers. It’s true that arrows behave much differently than grenades, but that becomes a matter of how the projectile object, not the firing weapon, is coded.
Rocket Launcher: this is basically like the grenade launcher, except it fires in a straight line. This would be for things like dart guns and laser pistols (not that I plan to have laser pistols, but the more you know, you know?).
Knife: and of course, the lowly knife. All melee weapons would be based on the knife. I haven’t actually looked into how melee works yet, far as programming. I’ve heard different ideas about it. Left 4 Dead‘s melee weapons fire raycasts to detect collisions. I think I can just ask the Unreal engine if the weapon itself is colliding with enemies. Either way, most melee weapons (save for stuff like hand tasers or mace) work the same way, you swing them and see if they hit anything.
One interesting differentiation that Valve makes in their games though is between weapons meant for clubbing and those meant for slashing/shredding. For instance, there are some melee weapons in TF2, usually sharp pointy ones, that cause players hit with them to “bleed”, stacking additional damage on top of that from the hit itself. In Left 4 Dead 2 (the first one didn’t have melee weapons), if you are fast enough and you are using a slashing/shredding weapon, you can free yourself from a Smoker’s tongue before he gets a good grip on you. Conversely, in PAYDAY 2, clubbing weapons tend to be more focused on knocking back enemies to give yourself more space than in actually dealing damage.
So yeah, in this way, all weapons evolve from these basic behaviors.