I’ve been pondering on something for a while, and I can’t really quite get the grasp on how to actually program weapons. Well, I know several ways I can do, but I’m not sure which is the best one and which is mostly use in the bigger games, for example Half-Life 2. After writing this, I’ll check on Google on how they do it.
The way I have it right now is as followed.
I have 2 classes and an Interface, a Firearm class, and a FirearmBuilder class. And an Interface called IEquipable with Equip and UnEquip functions. Later I will make an interface, and another 2 classes for throwable things. Such as handgrenades, and knifes.
Creating the weapon goes as followed, I have a list, and the list get’s initialized when the game gets launched. An example for making a weapon is this, which is happening in the GameController.
new FirearmBuilder()
.CreateFirearm("Pistol")
.withDamage(12.5f)
.withFireRate(0.5f)
.withInfiniteAmmo(true)
.WithMaxClipOf(12)
.withReloadTime(1.4f)
.withStartingAmmo(0)
.withWeaponId(1)
.withWeaponType(FireType.SemiAutomatic)
.withModelName("wm_pistol")
.Build()
The List is a list of the IEquipable interface, so everything that inherits IEquipable can be put there.
This is quite an easy way of making guns, I just need models, and sounds. I can then just make an inventory in the player class, and add weapons from the IEquipable list, if I advance that code a little, I can switch through all available weapons in my current inventory.
What this system not really allows is for special types of guns, for example in Black Ops Zombies there’s a gun that blasts zombies away. This manner of making guns won’t easily allow that without writing a WHOLE bunch of extra code, to allow that. But I’d figure.
Now for the shooting, and this is really the part where I get stuck. The player can shoot with the current weapon and damage everything that has a IDamagable interface on that gameObject. If it doesn’t, it of course doesn’t damage it. That’s as far I’ve gotten. I’ve got an idea on how to apply sound effects, muzzle flashes and that bunch of stuff. But I have no idea how to make this capable with special types of weapons, throwables and some other stuff. I also don’t know how I would handle the FireType enum, which include Semi_Automatic and Automatic.
This all started making me doubt this way of making weapons, as I do not want to do anything in the inspector but everything in code this makes me even more confused on how to do it. This is kind of getting long, but I hope you put through. I’m not asking for code, I’m asking for a better way on how to actually create weapons, and doing everything in code since I prefer that.
If you help me out with that, I would really appreciate it. Thanks in advance.
Before I posted this on this forums, I’ve posted it on the Unity3D subbreddit. And I got some interesting answers, I think I liked the one that this user used, with Frankstein code (as he said it). But there may be better ways of doing this, and I’m all for a little discussion!
P.S. Please don’t hesitate if you want more explanation about what I mean. I’m quite bad at explaining things but I’ll try my best.