Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 SYNYST3R1 · Aug 19, 2011 at 07:42 AM · arraymouselistweaponselection

Weapon selection system

 void Update () {
     if(Input.GetAxis("Mouse ScrollWheel")<0) {
         if (currentWeapon + 1 <= numWeapons){ 
             currentWeapon++;
             } else {
                 currentWeapon = 0;
             }
         SelectWeapon(currentWeapon);
         Debug.Log("Subtracted");
     }
     else if(Input.GetAxis("Mouse ScrollWheel")>0) {
         if (currentWeapon - 1 >= 0){ 
             currentWeapon--;
             } else {
                 currentWeapon = numWeapons;
             }
         SelectWeapon(currentWeapon);
         Debug.Log("Added");
     }
 }
 
 void SelectWeapon(int index) {
     for (int i=0;i<transform.childCount;i++)
     {
     // Activate the selected weapon
         if (i == index)
             transform.GetChild(i).gameObject.SetActiveRecursively(true);
         // Deactivate all other weapons
         else
             transform.GetChild(i).gameObject.SetActiveRecursively(false);
     }

 }

}

Ok so this is how I have my weapon selection set up. How would I make it so it can't switch to the weapon until it is picked up and equipped from my inventory. Would an array or list system work better? And how would I do that? I tried using them but I don't understand how to load the weapon once its in the array or list.

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
0
Best Answer

Answer by Bovine · Aug 19, 2011 at 04:24 PM

This is potentially quite a large question, so I'll try and briefly explain how we do things:

For our RPG we use a script that is serialisable and defines an Item. In the game world a similar script ItemInstance denotes an instance of an item, contains a reference to a concrete Item and any ancillary data such as if the weapon has charges or there are 5 of 10 (considering items that stack). The Item script itself contains detail such as the item type (i.e. a Mace) and what damage or armour value the item provides.

The Item itself has GameObject members that reference prefabs. For our game we instantiate the prefab to create the 3D object in the game world. We have a backpack which displays the icon representation by copying the script from the icon prefab for that Item (we use EZGUI so we actually copy the appearance of a disabled UIButton that's used for the icon image).

As I say this is quite a large topic, but you might have:

 List<Weapon> PlayerWeapons = new List<Weapon>();
 
 [System.Serializable]
 public class Weapon
 {
    public string WeaponName;
    public Texture WeaponIcon;
    public GameObject WeaponGeom;
 }

And then perhaps...

 [System.Serializable]
 public class WeaponInstance
 {
    public Weapon Weapon;
    public int BulletsInClip;
    public int BulletsRemaining;
 }

You can either set this up as an array somewhere else i.e. on an ItemManager and copy the iterms, or you could create an ItemList that subclasses a ScriptableObject that contains a list of items in your world, i.e.:

 public class ItemList : ScriptableObject
 {
    public List<Weapons> MasterWeaponList = new public List<Weapons>();
 }

You then need to be able to create this as an assert by extending the editor and using AssetDatabase.CreateAsset(). But the former may be easier - it depends on how you wish to access the data/flexibility/number of weapons.

Sorry if this is a can of worms, but I'd definitely use a List or array of Weapons somewhere and I'd have a corresponding List of either copies of those weapons or an instance that references them.

In your code above, you could always find the child item by some string from a structure that defines the weapon and thereby show/hide the appropriate ones.

Naturally we have a LOT of weapons and other items in our RPG so our system needs to be a little more full featured. You could just have an array of bools for your system and skip the weapon if the bool isn't true - to indicate the player has it. It's not very extensible however and has no real relationship to weapons you might pick up. Also, where do you store your bullets for a given weapon?

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 SYNYST3R1 · Aug 19, 2011 at 08:29 PM 0
Share

I've had it coded very similar to how you explained but where I get stuck is how do I use the mouse wheel to scroll through them and how do I make them show up based on the list.

For my inventory I have int cnt = 0 and if(cnt < inventory.Count) then it draws a button. Would it be something similar to show my weapons or no? And if it is would I just increase or decrease the int I make with the mouse wheel? I'm a bit confused on the whole process.

Also, I don't have any bullets yet. I only have melee weapons at the moment.

avatar image Bovine · Aug 19, 2011 at 09:51 PM 0
Share

Arrays tutorial:

http://msdn.microsoft.com/en-us/library/aa288453(v=vs.71).aspx

Generics:

http://msdn.microsoft.com/en-us/library/ms379564(v=vs.80).aspx

These are C# so if you need JavaScript you'll have tondo some digging yourself!

GL

avatar image
0

Answer by Bovine · Aug 19, 2011 at 09:09 PM

It really depends on how you want it to work: if you have 5 weapons then you might have

bool[] have_weapon = new bool[5];

Your scroll wheel is doing ++ or -- on your current_weapon but you would be better defining a NextWeapon() and PreviousWeapon() which look through your have_weapon array starting at your current_weapon until it finds one. NextWeapon() and PreviousWeapon() could return the index of the new weapon. If that differs from the current_weapon (they may only have 1 weapon) then you can hide current_weapon (find the relevant child if doing things as above) and show the new weapon, finally setting the current_weapon to the index of the new weapon.

Don't forget to wrap your NextWeapon() and PreviousWeapon() searches so that they go either to the end or the beginning depending on the direction. They too should stop searching when the index matches the current_weapon.

Naturally if you have a more complex weapon list/inventory, then you'll need to use something a little more verbose than a bool.

For example:

 void Update()
 {
     if(Input.GetAxis("Mouse ScrollWheel")<0)
     {
         int new_weap = PreviousWeapon();
         if(new_weap != m_current_weapon)
         {    SelectWeapon(new_weap);
         }
     }
     else if(Input.GetAxis("Mouse ScrollWheel")>0)
     {
         int new_weap = NextWeapon();
         if(new_weap != m_current_weapon)
         {    SelectWeapon(new_weap);
         }
     }
 }
 
 int NextWeapon()
 {   int ndx = m_current_weapon;
 
     do
     {    ++ndx;
         if(ndx == m_max_weapons) ndx = 0;
         if(m_have_weapon[ndx]) break;
     }
     while(ndx != m_current_weapon);
 
     return ndx;
 }

I'm sure you get the idea as to how PreviousWeapon() would work and its worth noting that if all your weapons are hidden, SelectWeapon() can hide the m_current_weapon, show the new_weapon and then set m_current_weapon to new_weapon - you don't need to run through all your weapon geometry.

I've moved this to another answer because Chrome was going berserk with the code blocks and it wasn't formatting correctly.

Cheers H

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 SYNYST3R1 · Aug 19, 2011 at 09:42 PM 0
Share

Oops didn't see this answer. Thanks, I will try this out

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

A node in a childnode? 1 Answer

Button showing when array is full 1 Answer

Array of Transform[] not correctly typed in Javascript 1 Answer

how to add list after removing some 1 Answer

Make a selection 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