• 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
1
Question by FTheCloud · Aug 27, 2011 at 07:58 PM · guninventoryswap

Best method for swaping guns and inventory

Have no experience with shooters whatsoever, except of course when i'm gaming >:), and I'm diving head first into development.

So, what's the best and most efficient way to swap guns and add to an inventory when buying guns?

Would it be slots, or arrays, activating and deactivating gameobjects, or instantiating and destroying gameobjects and see if they return true?

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

Answer by Rennat · Aug 27, 2011 at 09:25 PM

Depending on how you envision your weapon inventory working you might approach it in different ways. The best method for you will depend on how you've structured your project and the details of how you want it to work.

I've done up some quick examples of 2 common weapon systems in shooters. These won't work as drop in replacements but they should help you in figuring out the best method for your game.

Unreal or Half Life style weapon system

n this weapon system you can carry one of each weapon.

I would approach it by using an Enum for the weapon types and a boolean array for inventory.

 enum WeaponType {
     PISTOL,
     ASSAULT_RIFLE,
     MACHINE_GUN,
     SNIPER_RIFLE,
     ROCKET_LAUNCHER
 };
 
 var weaponInventory : boolean[];
 var currentWeapon : WeaponType = WeaponType.Pistol;
 
 function Start () {
     weaponInventory= new boolean[System.Enum.GetValues(WeaponType).Length];
 }
 
 function SwitchToWeapon (weapon : WeaponType) {
     if (weaponInventory[weapon]) {
         // animate putting currentWeapon away
         currentWeapon = weapon;
         // animate pulling out currentWeapon
     }
 }
 
 function PickupWeapon (weapon : WeaponType) {
     weaponInventory[weapon] = true;
     SwitchToWeapon(weapon);
 }
 
 function DropWeapon (weapon : WeaponType) {
     if (currentWeapon == weapon) {
         var nextWeaponIndex : int = weapon + 1;
         if (nextWeaponIndex >= System.Enum.GetValues(WeaponType).Length) {
             nextWeaponIndex = 0;
         }
         SwitchToWeapon(nextWeaponIndex);
     }
     weaponInventory[weapon] = false;
 }

Halo style weapon system

n this weapon system you have a set number of weapon slots (in this case 2) and you swap what gun is in the slot

 enum WeaponType {
     PISTOL,
     ASSAULT_RIFLE,
     MACHINE_GUN,
     SNIPER_RIFLE,
     ROCKET_LAUNCHER
 };
 
 var weaponSlots : int[] = new int[3] {
     WeaponType.PISTOL,
     WeaponType.ASSAULT_RIFLE
 };
 var currentWeaponIndex : int = 0;
 
 function NextWeapon () {
     var nextWeaponSlot : int = currentWeaponIndex + 1;
     if (nextWeaponSlot >= weaponSlots.Length)
         nextWeaponSlot = 0;
     // animate putting away currentWeaponIndex
     currentWeaponIndex = nextWeaponSlot;
     // animate pulling out currentWeaponIndex
 }
 
 function PreviousWeapon () {
     var nextWeaponSlot : int = currentWeaponIndex - 1;
     if (nextWeaponSlot == 0)
         nextWeaponSlot = weaponSlots.Length - 1;
     // animate putting away currentWeaponIndex
     currentWeaponIndex = nextWeaponSlot;
     // animate pulling out currentWeaponIndex
 }
 
 function SwapWeapon (weapon : WeaponType) {
     // animate putting away currentWeaponIndex
     weaponSlots[currentWeaponIndex] = weapon;
     // animate pulling out currentWeaponIndex
 }
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 FTheCloud · Aug 28, 2011 at 12:00 AM 0
Share

Thanks, helps alot

avatar image
0

Answer by silverblade1233 · Jul 12, 2016 at 06:47 PM

using UnityEngine; using System.Collections;

public class WeaponSwitch : MonoBehaviour { public GameObject M4A1; public GameObject Pistol;

 // Use this for initialization
 void Start () {
     M4A1.SetActive(true);
     Pistol.SetActive(false);
 }
 
 // Update is called once per frame
 void Update () {
     if (Input.GetKeyDown("1"))
     {
         M4A1.SetActive(true);
         Pistol.SetActive(false);
     }
     if (Input.GetKeyDown("2"))
     {
         M4A1.SetActive(false);
         Pistol.SetActive(true);
     }
 }

}

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 GarinR · Jul 12, 2016 at 07:11 PM 0
Share

This will only work if the $$anonymous$$4A1 and Pistol references are direct GameObjects.

Decent answer, but could use some setup and proper explanation of what's goin on.

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

7 People are following this question.

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

Related Questions

In-game Shop / Market/ Buying / inventory system? 3 Answers

Gun swap through collision 1 Answer

Gun swap through pickup? 2 Answers

ufps package freezes when adding inventory system 0 Answers

Different script for each item? 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