• 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 esitoinatteso · Oct 01, 2013 at 10:34 PM · weaponswapkey pressedswapping

Weapon Swap Script

Hi there! Right now I've got a Script that consists mainly of an Array in which I plan to store all my Weapons. What I'm trying to do is to let the player swap between all weapons in the Array, just by pressing LeftShift.

I managed to get the code working by myself until the very moment in which Weapons are forced to " switch back"... like: I'm at the end of the Array, when Shift is pressed again it's time to go back to the start.

For some reasons, the code you kindly wrote me doesn't get the job done either.

I'm now posting you my script, maybe the problem is inherent to logic itself... I'll restate what I'd like to do: write a script that handles all weapons in one array, gives the player the chance to swap between them ( later this will be done by power ups) and also gets the current weapon's stats and use them when pushed to shoot. Now that makes sense to me, but maybe it's not program-minded... or maybe I'm just stupid, who knows? maybe you know it!

 using UnityEngine;
 using System.Collections;
 
 public class WeaponManager : MonoBehaviour {
 
     public float hitDamage; // The Damage we want do deal with it
     public float fireRate; // The Speed at wich we shoot
     public float projectileSpeed; // This is the Speed of the bullet
     public GameObject currentWeapon; // Let's see.
     public float nextFire = 0.0f;// The delay till the next shot once fireRate is added.
     
     public GameObject [] weaponsArray = new GameObject[2];// This is our Array to store all the things. At the time I'm writing, I've got only 2 weapons as test.
     // I'll use Instructor and not code to specify what GameObject is element zero, one and so on...
     private int currentWeaponIndex = 0; // THIS INT IS NEEDED to keep track of the current weapon we are on it.
     
     
     
     // Use this for initialization
     void Start () {
         
         currentWeapon = weaponsArray[currentWeaponIndex]; // The weapon with which you start. Linking it with currentWeaponIndex let's us change it dinamically!
     
     }
     
     // Update is called once per frame
     void Update () {
         
         //Cheap Projectile Speed variable
         //currentWeapon.transform.Translate(Vector3.right * projectileSpeed * Time.deltaTime, Space.World); // it's working only if you attach this to the weapon itself. Maybe it's the same for Shooting...
         
         //Swap Weapon, just for testing sake
         if (Input.GetKeyDown(KeyCode.LeftShift)){
             
             currentWeaponIndex ++; // i.e. add 1 to our integer. Lets us swap between every element of our Array.
             
             if (currentWeaponIndex >= weaponsArray.Length){ // If we are trying to swap to an integer that is not in our Array, i.e. an integer outside our number's lenght.
                 
                 currentWeaponIndex = 0; // Current weapon index goes back to zero, we " loop".
                 currentWeapon = weaponsArray[currentWeaponIndex]; // your weapon equals to the one that your index says... i.e. zero right now.
                 
             }
             
         }
         
         //Shoot Sample
         if (Input.GetKeyDown(KeyCode.Space) && Time.time > nextFire){
             
             nextFire = Time.time + fireRate;
             currentWeapon.transform.position = new Vector3(PlayerV31.x, PlayerV31.y, PlayerV31.z);
             Instantiate(currentWeapon);        
         }
     }
 }

P.S.: at this time, instantiate works but it doesn't get the fire rate of the current weapon ( I know I haven't said him to do so but fact is I'm a bit confused on how to say it) AND the weapon swap doesn't work.

Thanks a lot for your time!

Comment
Add comment · Show 4
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 · Oct 01, 2013 at 11:24 PM 0
Share

Are you doing anything besides just setting currentWeapon? (like ar$$anonymous$$g it in your model) - why you said you can't go back? currentWeapon = weaponsArray[0];

avatar image gathos · Oct 01, 2013 at 11:50 PM 0
Share

this is just newbie sudo code, but could you possible get the array size with like

 int weptotal = (weaposarray.length);
 int currwep = 0;
 
 //then cycle through the weapons with an if..
  if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.LeftShift))
  currwep += 1;
  currentWeapon = weaponsarray[currwep];
 
 //reset back to 0 if you have cyckled through all weapons
 if (currwep > weptotal){
  currwep = 0;
 }
 } 

again i am a newbie but thought i would give my 2 cents. but they are canadian so they arnt worth much ;)

avatar image esitoinatteso · Oct 02, 2013 at 12:03 PM 0
Share

Hi gathos, I really appreciate your effort, unfortunately I get a CS0236 related to that line int weptotal = (weaponsarray.length);

vexe thanks for your comments, I'll edit my question to let you see my script

avatar image vexe · Oct 02, 2013 at 12:07 PM 0
Share

it's .Length, not .length - If you give a little bit more details as to what you wanna achieve, I'm sure I could help you a lot more.

2 Replies

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

Answer by vexe · Oct 02, 2013 at 02:06 PM

why are you setting the current weapon inside your if statement?

 if (currentWeaponIndex >= weaponsArray.Length){  
   currentWeaponIndex = 0;
   currentWeapon = weaponsArray[currentWeaponIndex];
 }

This should be:

 if (currentWeaponIndex >= weaponsArray.Length)
    currentWeaponIndex = 0;
 
 currentWeapon = weaponsArray[currentWeaponIndex];

Again, as I mentioned before, it would be better if you used the mod operator instead of all those checks, like:

 if (Input.GetKeyDown(KeyCode.LeftShift)){
    currentWeaponIndex = (currentWeaponIndex + 1) % weaponsArray.Length; 
    currentWeapon = weaponsArray[currentWeaponIndex];
 }

This will insure that the index will remain with the bounds of the array. And when the index reaches the (array.len-1), index+1 == array.len, so array.len % array.len == 0 and you get back to the beginning of the array.

A numeric example: you have 2 weapons, so arr.len = 2, at first you start with 0 for your current index (I'll refer to as index). when you press shift:

 index = (index+1) % arr.len = (0+1) % 2 = 1 % 2 = 1;

press shift again:

 index = (index+1) % arr.len = (1+1) % 2 = 2 % 2 = 0;

I would wrap this up in a method, SeekWeapons(SeekDirection dir) - here:

 public enum SeekDirection { Forward, Backward }
 
 public void SeekWeapons(SeekDirection dir) // this will also let you go backwards should you desire
 {
   if (dir == SeekDirection.Forward) 
      currentWeaponIndex = (currentWeaponIndex + 1) % weaponsArray.Length;
   else {
      currentWeaponIndex--;
      if (currentWeaponIndex < 0)
         currentWeaponIndex = weaponsArray.Length-1;
   }
   currentWeapon = weaponsArray[currentWeaponIndex];
 }

You might also need to instantly switch to a weapon (by pressing numbers or something), let's keep it simple and use the keyboard numbers, if you had 2 weapons, only num 1 and 2 will be used, if you have 5, num 1, 2, 3, 4, 5, etc...

 public void SwitchWeaponTo(int n)
 {
    n = Mathf.Clamp(n, 0, weaponsArray.Length-1); // this will make sure that n won't go above how many weapons you have, nor below 0
                              // Clamp works like this, if n > arr.len-1 -> return arr.len-1, if n < 0 -> return 0, else return n. See the docs for it if you don't know it
 
    currentWeapon = weaponsArray[n];
 }

Now, in your Update:

 void Update()
 {
    // code...
    if (Input.GetKeyDown(KeyCode.1)
    {
        currentWeaponIndex = 0;
    }
    else if (Input.GetKeyDown(KeyCode.2)
    {
        currentWeaponIndex = 1;
    }
    else if ... etc
 
    SwitchWeaponTo(currentWeaponIndex);
    // code...
 }


Couple of notes:

  1. You might get some compile errors if you copy-paste my code, adjust it accordingly to your needs (I prefer you take ideas, and not just copy-paste code)

  2. This whole system that you're using isn't gonna serve you well in the long run when your game gets complicated, for a better inventory/weapons design please see this link.

  3. An array might not serve you very well if you want to Add/Remove weapons, so you might consider using a List instead.

  4. Your politeness forced me to write you an answer, keep it up and good luck :)

Let me know if you get stuck somewhere, or if there was something you didn't understand. And as always, if an answer solves your problem please tick it as correct.

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 esitoinatteso · Oct 02, 2013 at 04:25 PM 0
Share

Thanks a lot Vexe! Everything you said to me is $$anonymous$$d-expanding! You addressed me to new interesting directions which I'll be sure to pursue, and this is awesome! $$anonymous$$any thanks again for your consideration, and also for the interesting link! I may bother you again on the same subject if I'm in dire need... hopefully I won't!

avatar image vexe · Oct 02, 2013 at 04:31 PM 1
Share

Sure thing man, just give a holla at yo playa :D

avatar image
0

Answer by TrickyHandz · Oct 02, 2013 at 12:59 AM

I would suggest doing this using a generic list rather than an array, since an array can't be resized easily. I've included a solution using your array which is fixed length and then a second using a list.

ARRAY EXAMPLE:

 using UnityEngine;
 using System.Collections;
 
 public class WeaponManager : MonoBehaviour
 {
 
     public GameObject currentWeapon;
 
     public GameObject[] weaponsArray = new GameObject[2];
 
     // An int to track which index item we
     // are assigning as the weapon
     private int currentWeaponIndex = 0;
 
     // Use this for initialization
     void Start()
     {
         // The weapon with which you start.
         currentWeapon = weaponsArray[currentWeaponIndex];
     }
 
     // Update is called once per frame
     void Update()
     {
 
         //Swap Weapon, just for testing sake
         if (Input.GetKeyDown(KeyCode.LeftShift))
         {
             currentWeaponIndex++;
 
             if (currentWeaponIndex >= weaponsArray.Length)
                 currentWeaponIndex = 0;
 
             currentWeapon = weaponsArray[currentWeaponIndex];
         }
     }
 }

LIST BASED SYSTEM:

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 
 public class WeaponManagerList : MonoBehaviour
 {
 
     public GameObject currentWeapon;
 
     // List to hold all our weapons
     public List<GameObject> weaponsList = new List<GameObject>();
     // An int to track which index item we
     // are assigning as the weapon
     private int currentWeaponIndex = 0;
 
     // Use this for initialization
     void Start()
     {
         // The weapon with which you start.
         currentWeapon = weaponsList[currentWeaponIndex];
     }
 
     // Update is called once per frame
     void Update()
     {
 
         //Swap Weapon, just for testing sake
         if (Input.GetKeyDown(KeyCode.LeftShift))
         {
             currentWeaponIndex++;
 
             if (currentWeaponIndex >= weaponsList.Count)
                 currentWeaponIndex = 0;
 
             currentWeapon = weaponsList[currentWeaponIndex];
         }
     }
 
     public void AddNewWeapons(GameObject newWeapon)
     {
         weaponsList.Add(newWeapon);
     }
 
     public void RemoveWeapon(GameObject weapon)
     {
         if (weaponsList.Contains(weapon))
             weaponsList.Remove(weapon);
     }
 }

Since I use Lists a lot, I included a little snippet that will allow you to add a weapon or remove a weapon from the list.

Comment
Add comment · Show 3 · 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 · Oct 02, 2013 at 01:04 AM 1
Share

You and the other comment in the question have the same mistake:

 if (currentWeaponIndex > weaponsList.Count)
   currentWeaponIndex = 0;

it should be '>='

Ins$$anonymous$$d of all that, just use:

 currentWeaponIndex = (currentWeaponIndex + 1) % weaponsList.Count;

That will ensure that you remain inside the bounds.

avatar image TrickyHandz · Oct 02, 2013 at 01:35 AM 0
Share

Good catch @vexe, I corrected the code. Also, as a note, using $$anonymous$$odulo in this case would lock the index at 0 when there is no remainder, and skip indexes if the list increases in size.

avatar image esitoinatteso · Oct 02, 2013 at 12:06 PM 0
Share

Hi TrickyHandz and thanks a lot for your kind answers, unfortunately I tried to understand the code and then paste it to $$anonymous$$e but i doesn't work. $$anonymous$$aybe it's something I'm missing like vexe said, like ar$$anonymous$$g my weapon to my player. I'm going to edit the question to let you see the full script, maybe it's helpful to better understand it!

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

18 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

Related Questions

Swapping Items in List Replaces Value Instead 1 Answer

How to swap weapons easily? 1 Answer

Swapping one model for a prefab 2 Answers

Swap player positions 1 Answer

how to change the gun but keep the bullets 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