Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
3
Question by Coyote · Aug 13, 2013 at 02:42 AM · weaponsrecoilaccuracy

Managing different weapons in scripting.

I am currently planning and working on a FPS project. My question is: What is the best way to manage information about weapons in my script. For example the accuracy, recoil, delay before you can fire again and so on. I was thinking about setting up multiple arrays, and use the array IDs as IDs for different weapons. So I would create an array with all my weapons, so the Element 0 would be the weapon of ID 0. Then I would create another array for my accuracy. And again, the Element 0 would be the accuracy for the weapon with the ID 0. Is this a good way to do this? Is there any other way. Can I make a new script for each weapon containing the variables? Any help will be appreciated!

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image getyour411 · Aug 13, 2013 at 03:16 AM 0
Share

Perhaps you could create a class called RangedWeapon with properties like accuracy, recoil, delay etc and attach that to the weapons

4 Replies

· Add your reply
  • Sort: 
avatar image
8
Best Answer

Answer by Jamora · Aug 13, 2013 at 12:58 PM

vexe's solution works, brilliantly, but may cause you to create empty classes because the extending class might not add any functionality. Additionally, when doing deep inheritance like this you need to keep the Liskov Substitution Principle in mind; especially if none of the classes are abstract. It prevents sometimes hair-pullingly strange bugs where gameplay breaks in seemingly random places.

It basically says don't change the behavior of methods in subclasses. For example, if the Reload() of class Firearm only works correctly when the weapon is empty, then the SMG class should not override the method such that it works with a bullets left in the clip.

Depending on your project and its needs, a more favorable approach might be to apply Composition over Inheritance. Basically, you decide what behavior your weapons have as interfaces and then implement all the appropriate interfaces for your weapon classes. Basically something like this:

alt text

This way you will not have any classes just to support a hierarchy, only classes that are needed and do not really depend on any other classes. Of course this doesn't exclude you from inheriting classes. You can of course have a base Firearm class that your firearms extend if all firearms truly share any (exactly the same)behavior.


classes.png (7.4 kB)
Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image vexe · Aug 13, 2013 at 01:13 PM 1
Share
  • really nice! - Really good use of interfaces. I actually shared him a part of the weapons, in my items system I'm working on for my game. I've used interfaces but not with weapons, they're really cool. But since my game isn't very much weapon-oriented like an FPS, the use of basic inheritance covered my weapon usage. If the OP could learn to reach a level where he understand your solution, it would be great!

avatar image vexe · Oct 13, 2013 at 09:17 AM 0
Share

@Jamora I was actually going to comment, but then I thought it would lead to an open-discussion, which I took here. Take a look at it if you can. Thanks.

avatar image
4

Answer by vexe · Aug 13, 2013 at 11:58 AM

You obviously don't know about OOP. The way you're approaching is really not the way to go. I suggest you first take the time and learn OOP (what's a class, inheritance, polymorphism, interfaces abstraction, virtualizim, etc and a good programming language well. C# preferably and then get back to Unity)

You basically need to make a "Weapon" class, and inherit all the other weapon types from it. Put the common stuff of all weapons inside that class, then you could branch to "FireArm" and "Melee" - I will share a simple design with you, have a look:

alt text

In your child classes, you override what you need from the parents, like Reload for example, because not all weapons reload in the same way, and also the UseAnimation which is dif in each weapon. Once you setup this whole system, you just attach the appropriate script to your weapon gameobject. You could also let your player have a list of weapons, then you can dynamically add/remove weapons.

Once you know OOP, you will clearly see the right path. Good luck.


simpledesign.png (24.2 kB)
Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image daniel.mori · Oct 20, 2013 at 02:55 AM 1
Share

The above solution is helpful, But I personally, wouldn't include the 'automatic' and 'semi-auto' classes. This should be considered 'properties of a type of weapon'...not a type itself. Be strict and stick to 'types of weapon'.

avatar image d-sangria119 · Jan 19, 2017 at 12:31 PM 0
Share
  • for what @daniel.mori said.

For anyone reading this looking for a system to copy directly into their game, definitely sticking to "types of weapon" is better than dividing into small properties like "semi-automatic" and "automatic."

I guess that @vexe was separating it to illustrate his point, and what a brilliant example he gave!

avatar image
0

Answer by Coyote · Aug 18, 2013 at 11:41 PM

So I learned about OOP. One thing I don't understand is, how do I assign a weapon to a class? Like if I have a Pistol. How do I make it use the Pistol class?

What I currently have: using UnityEngine; using System.Collections;

 public class Weapons : MonoBehaviour 
 {
     
 }
 
 public class Melee : Weapons
 {
     
 }
 
 public class Firearm : Weapons
 {
     
 }
 
 public class SemiAutomatic : Firearm
 {
     
 }
 
 public class Automatic : Firearm
 {
     
 }
 
 
 // SEMI-AUTOMATIC
 
 public class Pistol : SemiAutomatic
 {
 
 }
 
 
 // AUTOMATIC
 
 public class Rifle : Automatic
 {
     
 }
Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image vexe · Aug 19, 2013 at 04:02 AM 0
Share

O$$anonymous$$, first you put the common stuff that all your weapons will share in your "Weapon" class, like damage, name, etc. Next what all the firearms will share in the "Firearm" class. Note you don't need to put name, damage, etc in the firearm, because it's inheriting from "Weapon", which means it gets to have its protected and public members. After you're done putting the right stuff in their right place, all you do is just assign for example your "Pistol" script to your pistol gameobject. For that you could make an empty gameObject, and have the weapon model a child to that object, and let the script be attached to it, make it a prefab if you like.

avatar image
0

Answer by mahdiii · Jul 08, 2016 at 11:10 AM

OOP approach is not good alone. It has been explained several times before. Use component based approach + OOP In unity. Write health component,damage component, weapon component,etc

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

21 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

FPS Gun Recoil (Accuracy) 1 Answer

How to make Recoil Dampening aka. Recentering? 0 Answers

Player Weapon Scripting 1 Answer

Creating an inventory/weapon array and ability to scroll through it 1 Answer

Firearm tutorial 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges