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
0
Question by BrimTheOne · Aug 12, 2013 at 05:02 PM · modelclassesenumweapons

C# Weapon class generation

Hello everybody, i am working on learning about classes and such, and ive tried to make my own. using UnityEngine; public class Weapons { private WeaponTypes TypeOfWeapon; private Rarity howRare; private WeaponKind KindOfWeapon; private int Range;

     public Weapons(){
         TypeOfWeapon = WeaponTypes.Melee;
         howRare = Rarity.Common;
         KindOfWeapon = WeaponKind.Axe;
     }
 }
 
 public enum WeaponTypes{
     Melee,
     Ranged,
     Magic
 }
 public enum WeaponKind{
     Sword,
     Axe,
     Bow,
     CrossBow,
     Wand,
     Staff
 }
 public enum Rarity{
     Common,
     UnCommon,
     Rare,
     UberRare
 }

and it is fine for now. my question is : I want to make a weapon be generated from these information. How would i do? would i make a different script to do so? would i make a script that inherits from this class for each weapon kind i want? and attach it on script? also, how would i go around attaching diffrent models for each weaponkind enum?

Thanks

Comment
Add comment
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

2 Replies

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

Answer by YoungDeveloper · Aug 12, 2013 at 05:35 PM

This randomly generates the item.

 public WeaponType weapon;
 public WeaponKind kind;
 public Rarity rarity;
 
 
 void Start(){
 weapon = GetRandomEnum<WeaponType>();
 kind = GetRandomEnum<WeaponKind>();
 rarity = GetRandomEnum<Rarity>();
 }

There are very many ways of how could you manage your scripts. This is where you have to think yourself, how you will manage your game system. Simplest way is to include everything for each item, all enums, classes etc. Maybe there could be one class (script) with all the information about what the weapon/item could be, and in each item script just get that script as component and generate the randomness.

I made them public so you could see them in inspector.

Comment
Add comment · Show 18 · 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 BrimTheOne · Aug 12, 2013 at 06:29 PM 0
Share

Ok thanks for the answer, it is helpful. Could you tell me how i would go about asigning a model to it? And make a diffrent script with stats to it? dmg and such?

avatar image YoungDeveloper · Aug 12, 2013 at 06:58 PM 0
Share

I would create script for each weapon type separately, like one for melee, one for magic staffs, shields, etc. With the code like this:

 //One universal script for melee weapons
 
 public string name;
 public int damage; // or float, depends on your game
 public int defense;
 public int weight;
 public int firedmg;
 public int frostdmg;
 //etc;
 
 //Still could be useful, if you will have an inventory system, you could store each items size. So you could know, can you, or can you not pick it up.
 
 public int size_x;
 public int size_y;
 
 private int DEFAULT = 0; //We need this because, not every weapon will have fire and other damage, right? So at the start we will give everything a zero, which will mean, it don't have that ability.
 
 void Start(){
    damage=defense=weight=firedmg=frostdmg=size_x=size_y = DEFAULT;
 }

So from here, you are pretty much done, and you can easily add all the variables in the inspector, first attach the script to new object, and edit. Leaving zero where you don't want anything.

You can create a new weapon, by creating a new empty game object first, call it as you want, then attach the script and any model you want, and there you have it, a weapon with its own stats.

avatar image BrimTheOne · Aug 12, 2013 at 07:23 PM 0
Share

Thanks alot, but is there a way to do like this ? (pseudocode) if(weapon = melee){ if(kind = sword){ InstatiateOneOfThese3RandomSword$$anonymous$$odels; } }

avatar image YoungDeveloper · Aug 12, 2013 at 08:01 PM 0
Share

Sure, as i mentioned in my first reply, you can also add type, for each script-weapon. So if you have type, you can check what kind of type is it.

So let's say if we have these enum functions for this weapon.

     public GameObject model;
        
     public enum Weapon$$anonymous$$ind{
     Sword,
     Axe,
     Bow,
     CrossBow,
     Wand,
     Staff
     }
     
     public enum Rarity{
     Common,
     UnCommon,
     Rare,
     UberRare
     }
     
     public Weapon$$anonymous$$ind kind;
     public Rarity rarity;
     //Then you assign them in the inspector
 
 void Start(){
     //And this is how you search for what kind is it
     if(kind == Weapon$$anonymous$$ind.sword){
        Debug.Log("Yep, It's a sword");
        if(rarity == Rarity.Rare){
           Debug.Log("It's a rare sword, Yay I'm rich!");
           //Here goes anything you want.
     
           //So by this you can find anything.
     
               //You could create a function which searches for random 3d model, but this function should be seperate script because you will have to make a long list of 3d models, array of strings or gameobject, in this example i use string.
     
            model = new GameObject( FindRandomSword() );
     //so this will make a new GO called Dwarf_Axe
     
         }
     }
 }


In other script..

 string FindRandomSword(){
 ...random number a;
 ..bla
 ..bla
 ..bla
 return array[a]; //which lets say is "Dwarf_Axe"
 
 
 }


avatar image BrimTheOne · Aug 12, 2013 at 09:30 PM 0
Share
 public WeaponTypes weapon;
 public $$anonymous$$eleeWeapon$$anonymous$$ind kind;
 public Rarity rarity;
 
 GameObject weaponmodeluseAxeTier1;
 
 public GameObject[] firstTierAxe = new GameObject[2];
 public GameObject firstTier1Axe;
 public GameObject firstTier2Axe;    
 
  static T GetRandomEnum<T>()
     
 {
 System.Array A = System.Enum.GetValues(typeof(T));
 T V = (T)A.GetValue(UnityEngine.Random.Range(0,A.Length));
 return V;
 }
 
 void Start(){
 weapon = GetRandomEnum<WeaponTypes>();
     if(weapon == WeaponTypes.$$anonymous$$elee)
     {
     kind = GetRandomEnum<$$anonymous$$eleeWeapon$$anonymous$$ind>();
         if(kind == $$anonymous$$eleeWeapon$$anonymous$$ind.Axe){

             GameObject weapon$$anonymous$$odel;
             weaponmodeluseAxeTier1 = Random.Range(0, firstTierAxe.Length);;
              weapon$$anonymous$$odel = Instantiate(weaponmodeluseAxeTier1,transform.position, transform.rotation)    as GameObject;
             Debug.Log("Yaya");
         }
         else
         {
             Debug.Log("snap");
         }
     }
     else if(weapon == WeaponTypes.Ranged)
     {
                         Debug.Log("sorry, RANGED");
         //kind = GetRandomEnum<RangedWeapon$$anonymous$$ind>();
     }
     else if(weapon == WeaponTypes.$$anonymous$$agic)
     {
                         Debug.Log("sorry, magic");
         //kind = GetRandomEnum<$$anonymous$$agicWeapon$$anonymous$$ind>();
     }

 rarity = GetRandomEnum<Rarity>();
 }

}

I have got this so far, could you show me what to do to get a random here maybe? i know i ask alot of questions, im sorry :i

Show more comments
avatar image
0

Answer by Ouija · Aug 13, 2013 at 08:51 PM

If you download this project, you might find some scripts that interest you. It has a great weapons selection script you could learn from. http://www.youtube.com/watch?v=VgY6NNGfnlA

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

17 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

Related Questions

Multiple Cars not working 1 Answer

How to rotate shader/texture? 1 Answer

Handling different combinations of enum selection 1 Answer

Creating a Loadout 1 Answer

Ragdoll script vs Extra ragdoll model 0 Answers


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