- Home /
Best way to make a ton of weapons,Best way to build a ton of weapons
Hi there! Im building a 2d pvp platformer and Im planning to build a ton of drastically different weapons. My question is: what would be the best way to achieve this? Of course I could make tens of different weapon prefabs with tens of different scripts but something tells me theres a better way. Could someone point me in the right direction? Thanks!,Hey! I am building a 2D pvp platformer and Im planning to make a ton of drastically different weapons. My question is: what would be the most efficient way of doing this? I could obviously just make tens of different prefabs with tens of different scripts but I know there should be a more modular way. Could anyone point me in the right direction?
Answer by Captain_Pineapple · Jun 16, 2018 at 09:40 AM
Hey there,
depends on what the differences between those weapons that you intend to have. So when you look at games like Borderlands or Fallout, a wepon consists of up to 7 Parts or something. Then you can have a pool of lets say 10 different parts for each Part-Type and randomly create a weapon out of this pool. So given 7 different types (e.g. Barrel, Trigger, Grip, Stock, Sight, Magazin, Ammunition ... ) with 10 different parts you could randomly put together 10.000.000 Weapons. Each can then either have a base stat or a modifier towards some weapon stat. For example the trigger could give you a base fire rate and the barrel and magazin will modify this value so that in the end the weapons stats are a combination of all the Parts values.
Limitations at this point are your creativity to what extend you can come up wth different parts and the balancing. In my current project i allow the user to build custom weapons from the system i described above. Balancing proves really difficult since there will be really crappy weapons and some overpowered ones and you might not be able to find these.
To implement this i'd suggest to first think about the construction of the weapons. You will Probably have some Weapon Class that is attached to some empty Transform. This will be the base of your weapons. Then each Weaponpart can be a subObject of this weapon and in the end look like a whole weapon. Since each barrel will be of different length you probably want to create some projectileorigin that is attached to the barrel wich gives you a direction to fire in and a point where you create Bullets.
I hope this helps so far. In case you have more questions feel free to ask.
Thanks for the reply! $$anonymous$$y weapons will be a bit different though. I am thinking about weird guns like freezerays and shrink guns. Would the same technique youre using work for that?
Well yes in theory this is applicable. In my case the player can choose between grenades, lasers, normal bullets and "sniperProjectiles". These bullets are a script themselve that has a base damage value. Then the weapon modifies this damagevalue depending on attached weaponparts. As you can see for my case i have rather normal projectiles but they also have their own behaviour (splash damage/bulletdrop/projectilespeed)
You should probably have a weaponpart that describes your projectiles/lasers behaviour. So your weapon has a variable of type "ProjectileType". This will be your projectiles base class. Then you derive all the projectiles you want from this ProjectileType class. The thing they have in common? -> give them all a fire()
function. Then you can have your player trigger the weapon which itself calls it's projectiles ´fire()´ function.
Then you can have your attached projectile do whatever it should do from the fire()
function.
Normal bullet? spawn a prefab, apply velocity to it.
A laser? do a raycast, do some particle effects.
Special effects like freezing? Since you handle the raycast in the firefunction just apply the freezing effect from there. Or downscale the hit transform for shrinking.
You will have to code all bullet behaviour by hand in it's own script though.
Your answer
Follow this Question
Related Questions
Serialize a list of scriptable objects to Json 0 Answers
Instantiate with random scriptableObject 2 Answers
How can I make an object stop all momentum and hold it's position in air? 1 Answer
2D Mouse Aiming on Unity 4.3 1 Answer
2d enemy ai 0 Answers