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 /
  • Help Room /
avatar image
0
Question by Cryno1000 · Apr 15, 2020 at 11:06 AM · c#scripting problemweaponweapon system

Having a few script errors that I ran into.

I recently overhauled a script that handles all of the weapons in my game. But now whenever I swap weapons it takes several seconds to be able to shoot and I can't find it out. If anyone can help it would be greatly appreciated. (P.S. Any tips to clean up my code a bit would also be really nice!)

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.UI;
 using UnityEngine.SceneManagement;
 
 public class barrel : MonoBehaviour
 {    
     [SerializeField]
     private Transform barrelTip;
 
     [SerializeField]
     private Transform barrelTip2;
 
     [SerializeField]
     private Transform barrelTip3;
 
     public GameObject bullet;
     public GameObject rocket;
 
     private Vector2 lookDirection;
     private float lookAngle;
 
     public bool canShootWeapon1;
     public bool canShootWeapon2;
     public bool canShootWeapon3;
     public bool canShootCurrentWeapon;
 
     public bool reloading = false;
 
     public int prevreserve;
 
     public playercontroller player;
 
     public Text ammo;
 
     public int shootspeed;
     public int amounttoremove;
 
     public int currentweaponammo;
     public int weapon1ammo = 12;
     public int weapon2ammo;
     public int weapon3ammo;
 
     public int currentweaponreserve;
     public int weapon1reserve;
     public int weapon2reserve;
     public int weapon3reserve;
 
     public int maxcurrentweaponammo;
     public int maxweapon1ammo = 12;
     public int maxweapon2ammo;
     public int maxweapon3ammo;
 
     public int maxpistolammo = 12;
     public int maxassaultrifleammo = 30;
     public int maxsniperammo = 5;
     public int maxshotgunammo = 5;
     public int maxlmgammo = 100;
     public int maxsmgammo = 30;
     public int maxrocketlauncherammo = 1;
 
     public bool keyboard;
 
     public Transform target;
     
     //controls
     public string shoot;
 
     public string currentPrefix1;
     public string currentPrefix2;
 
     //buffs
     public bool piercing = false;
     public bool metal = false;
     public bool grippy = false;
     public bool bouncy = false;
     public bool crippling = false;
     public bool lightbool = false;
     public bool legendary = false;
 
     //debuffs
     public bool paper = false;
     public bool sticky = false;
     public bool homemade = false;
     public bool warped = false;
     public bool broken = false;
     public bool taxing = false;
     public bool heavy = false;
 
     public float pistolwait = 3;
     public float assaultriflewait = 3;
     public float sniperwait = 3;
     public float shotgunwait = 3;
     public float lmgwait = 3;
     public float smgwait = 3;
     public float rocketlauncherwait = 3;
 
     //def == default
     public float defpistolwait = 3;
     public float defassaultriflewait = 3;
     public float defsniperwait = 3;
     public float defshotgunwait = 3;
     public float deflmgwait = 3;
     public float defsmgwait = 3;
     public float defrocketlauncherwait = 3;
     public float currentweaponwait;
 
     public float gripwait = 0.8f;
     public float stickywait = 1.2f;
 
     [HideInInspector] public bool weapon2owned;
     [HideInInspector] public bool weapon3owned;
     [HideInInspector] public string weapon1name;
     [HideInInspector] public string weapon2name;
     [HideInInspector] public string weapon3name;
     [HideInInspector] public string weapon1type;
     [HideInInspector] public string weapon2type;
     [HideInInspector] public string weapon3type;
     [HideInInspector] public string weapon2prefix1;
     [HideInInspector] public string weapon2prefix2;
     [HideInInspector] public string weapon3prefix1;
     [HideInInspector] public string weapon3prefix2;
     [HideInInspector] public string currentweaponprefix1;
     [HideInInspector] public string currentweaponprefix2;
     public string currentweapontype;
     [HideInInspector] public string currentweaponname;
 
     public Text currentweapon;
 
     public Sprite pistolimg;
     public Sprite assaultrifleimg;
     public Sprite shotgunimg;
     public Sprite sniperimg;
     public Sprite lmgimg;
     public Sprite smgimg;
     public Sprite rocketlauncherimg;
 
     public Image weapon;
 
     public int weaponselection;
 
     public string reload;
 
     public weaponbox weaponboxscript;
 
     void Start()
     {
         player.GetComponent<playercontroller>();
         shootspeed = 20;
 
         weaponselection = 1;
         currentweaponammo = maxpistolammo;
         WeaponSwapFunction();
 
         canShootWeapon1 = true;
         canShootWeapon2 = true;
         canShootWeapon3 = true;
         canShootCurrentWeapon = true;
 
         weapon1type = "Pistol";
         weapon2type = "none";
         weapon3type = "none";
     }
 
     // Update is called once per frame 
     void Update()
     {
         if (Input.GetButtonDown(reload)) StartCoroutine("reloadcheck");
 
         GetRefsBarrel();
         AttackCheck();
         RotateToLookDirection();
         WeaponSprites();
 
         Scene currentScene = SceneManager.GetActiveScene();
         string sceneName = currentScene.name;
         if (sceneName != "charactercreation")
         {
             if (weaponselection == 1) 
             {
                 currentweapontype = weapon1type;
                 currentweaponname = weapon1name;
             }
             if (weaponselection == 2)
             {
                 currentweapontype = weapon2type;
                 currentweaponname = weapon2name;
             }
             if (weaponselection == 3)
             {
                 currentweapontype = weapon3type;
                 currentweaponname = weapon3name;
             }
             currentweapon.text = currentweaponname;
         }
     }
 
     public void CanShootThisWeapon()
     {
         if (weaponselection == 1 && canShootWeapon1) canShootCurrentWeapon = true;
         else if (weaponselection == 2 && canShootWeapon2) canShootCurrentWeapon = true;
         else if (weaponselection == 3 && canShootWeapon3) canShootCurrentWeapon = true;
         else canShootCurrentWeapon = false;
     }
     
     public void GetCurrentReserve()
     {
         if (weaponselection == 1) currentweaponreserve = weapon1reserve;
         else if (weaponselection == 2) currentweaponreserve = weapon2reserve;
         else if (weaponselection == 3) currentweaponreserve = weapon3reserve;
     }
 
     public void WeaponSwapFunction()
     {
         if (weaponselection == 0)
         {
             weaponselection = 3;
             WeaponSwapFunction();
         }
         else if (weaponselection == 1)
         {
             currentweapontype = weapon1type;
             currentweaponprefix1 = "";
             currentweaponprefix2 = "";
             currentweaponammo = weapon1ammo;
             CallWeaponSwapFunctions();
         }
         else if (weaponselection == 2)
         {
             if (weapon2type == "none")
             {
                 weaponselection = 1;
                 WeaponSwapFunction();
             }
             else if (weapon2type != "none")
             {
                 currentweapontype = weapon2type;
                 currentweaponprefix1 = weapon2prefix1;
                 currentweaponprefix2 = weapon2prefix2;
                 currentweaponammo = weapon2ammo;
                 CallWeaponSwapFunctions();
             }
         }
         else if (weaponselection == 3)
         {
             if (weapon3type == "none")
             {
                 weaponselection = 2;
                 WeaponSwapFunction();
             }
             else if (weapon3type != "none")
             {
                 currentweapontype = weapon3type;
                 currentweaponprefix1 = weapon3prefix1;
                 currentweaponprefix2 = weapon3prefix2;
                 currentweaponammo = weapon3ammo;
                 CallWeaponSwapFunctions();
             }
         }
         else if (weaponselection == 4)
         {
             weaponselection = 1;
             WeaponSwapFunction();
         }
         Scene currentScene = SceneManager.GetActiveScene();
         string sceneName = currentScene.name;
         if (sceneName != "charactercreation") currentweapon.text = currentweaponname;
     }
 
     public void WeaponSprites()
     {
         if (currentweapontype == "Pistol") weapon.sprite = pistolimg;
         else if (currentweapontype == "Assault Rifle") weapon.sprite = assaultrifleimg;
         else if (currentweapontype == "Shotgun") weapon.sprite = shotgunimg;
         else if (currentweapontype == "Sniper") weapon.sprite = sniperimg;
         else if (currentweapontype == "LMG") weapon.sprite = lmgimg;
         else if (currentweapontype == "SMG") weapon.sprite = smgimg;
         else if (currentweapontype == "Rocket Launcher") weapon.sprite = rocketlauncherimg;
     }
 
     public void WeaponNames()
     {
         weapon1name = "Pistol";
         weapon2name = weapon2prefix1 + weapon2prefix2 + weapon2type;
         weapon3name = weapon3prefix1 + weapon3prefix2 + weapon3type;
     }
 
     public void WeaponBox()
     {
         weaponboxscript.randomweapon();
         if (weaponselection == 2 && weapon3owned == true || weaponselection == 1 && weapon2type == "none")
         {
             weapon2type = weaponboxscript.weapontype;
             weapon2prefix1 = weaponboxscript.prefix1choice;
             weapon2prefix2 = weaponboxscript.prefix2choice;
             weaponselection = 2;
             WeaponNames();
             UpdateAmmoAndShootSpeed();
             weapon2owned = true;
             WeaponSwapFunction();
             GetAndSetMaxAmmo();
         }
         if (weaponselection == 3 || weaponselection == 2 && weapon3type == "none")
         {
             weapon3type = weaponboxscript.weapontype;
             weapon3prefix1 = weaponboxscript.prefix1choice;
             weapon3prefix2 = weaponboxscript.prefix2choice;
             weaponselection = 3;
             WeaponNames();
             UpdateAmmoAndShootSpeed();
             weapon3owned = true;
             WeaponSwapFunction();
             GetAndSetMaxAmmo();
         }
     }
 
     public void GetAndSetMaxAmmo()
     {
         if (currentweapontype == "Pistol") currentweaponammo = maxpistolammo;
         else if (currentweapontype == "Assault Rifle") currentweaponammo = maxassaultrifleammo;
         else if (currentweapontype == "Shotgun") currentweaponammo = maxshotgunammo;
         else if (currentweapontype == "Sniper") currentweaponammo = maxsniperammo;
         else if (currentweapontype == "LMG") currentweaponammo = maxlmgammo;
         else if (currentweapontype == "SMG") currentweaponammo = maxsmgammo;
         else if (currentweapontype == "Rocket Launcher") currentweaponammo = maxrocketlauncherammo;
 
         if (weaponselection == 2)
         {
             if (currentweapontype == "Pistol") weapon2ammo = maxpistolammo;
             else if (currentweapontype == "Assault Rifle") weapon2ammo = maxassaultrifleammo;
             else if (currentweapontype == "Shotgun") weapon2ammo = maxshotgunammo;
             else if (currentweapontype == "Sniper") weapon2ammo = maxsniperammo;
             else if (currentweapontype == "LMG") weapon2ammo = maxlmgammo;
             else if (currentweapontype == "SMG") weapon2ammo = maxsmgammo;
             else if (currentweapontype == "Rocket Launcher") weapon2ammo = maxrocketlauncherammo;
         }
         else if (weaponselection == 3)
         {
             if (currentweapontype == "Pistol") weapon3ammo = maxpistolammo;
             else if (currentweapontype == "Assault Rifle") weapon3ammo = maxassaultrifleammo;
             else if (currentweapontype == "Shotgun") weapon3ammo = maxshotgunammo;
             else if (currentweapontype == "Sniper") weapon3ammo = maxsniperammo;
             else if (currentweapontype == "LMG") weapon3ammo = maxlmgammo;
             else if (currentweapontype == "SMG") weapon3ammo = maxsmgammo;
             else if (currentweapontype == "Rocket Launcher") weapon3ammo = maxrocketlauncherammo;
         }
     }
 
     public void GetRefs()
     {
         weaponboxscript = GameObject.FindGameObjectWithTag("items/weaponbox").GetComponent<weaponbox>();
         if (player.player == 1)
         {
             currentweapon = GameObject.FindGameObjectWithTag("ui/currentweapon/currentweapon1").GetComponent<Text>();
             weapon = GameObject.FindGameObjectWithTag("ui/weapon/weapon1").GetComponent<Image>();
         }
         else if (player.player == 2)
         {
             currentweapon = GameObject.FindGameObjectWithTag("ui/currentweapon/currentweapon2").GetComponent<Text>();
             weapon = GameObject.FindGameObjectWithTag("ui/weapon/weapon2").GetComponent<Image>();
         }
         else if (player.player == 3)
         {
             currentweapon = GameObject.FindGameObjectWithTag("ui/currentweapon/currentweapon3").GetComponent<Text>();
             weapon = GameObject.FindGameObjectWithTag("ui/weapon/weapon3").GetComponent<Image>();
         }
         else if (player.player == 4)
         {
             currentweapon = GameObject.FindGameObjectWithTag("ui/currentweapon/currentweapon4").GetComponent<Text>();
             weapon = GameObject.FindGameObjectWithTag("ui/weapon/weapon4").GetComponent<Image>();
         }
         currentweapon.text = currentweaponname;
     }
 
     public void RotateToLookDirection()
     {
         if (keyboard == true)
         {
             lookDirection = Camera.main.ScreenToWorldPoint(Input.mousePosition) - transform.position;
             lookAngle = Mathf.Atan2(lookDirection.y, lookDirection.x) * Mathf.Rad2Deg;
             transform.rotation = Quaternion.Euler(0f, 0f, lookAngle - 90f);
         }
 
         if (keyboard == false)
         {
             Vector3 difference = target.position - transform.position;
             float rotationZ = Mathf.Atan2(difference.y, difference.x) * Mathf.Rad2Deg;
             transform.rotation = Quaternion.Euler(0.0f, 0.0f, rotationZ);
         }
     }
 
     public void AttackCheck()
     {
         float attack = Input.GetAxis(shoot);
 
         if (Input.GetButtonDown(shoot) || attack >= 0.1f)
         {
             if (currentweapontype == "Pistol" || currentweapontype == "Shotgun" || currentweapontype == "Sniper" || currentweapontype == "Rocket Launcher")
             {
                 if (canShootCurrentWeapon == true)
                 {
                     if (weaponselection == 1)
                     {
                         weapon1ammo -= 1;
                         canShootCurrentWeapon = false;
                     }
                     else if (weaponselection == 2)
                     {
                         weapon2ammo -= 1;
                         canShootCurrentWeapon = false;
                     }
                     else if (weaponselection == 3)
                     {
                         weapon3ammo -= 1;
                         canShootCurrentWeapon = false;
                     }
                     FireBullet();
                     currentweaponammo -= 1;
                     UpdateAmmoAndShootSpeed();
                     StartCoroutine("shootwait");
                 }
             }
             else { }
         }
         if (Input.GetButton(shoot) || attack >= 0.1f)
         {
             if (currentweapontype == "LMG" || currentweapontype == "SMG" || currentweapontype == "Assault Rifle")
             {
                 if (canShootCurrentWeapon == true)
                 {
                     if (weaponselection == 1)
                     {
                         weapon1ammo -= 1;
                         canShootCurrentWeapon = false;
                     }
                     else if (weaponselection == 2)
                     {
                         weapon2ammo -= 1;
                         canShootCurrentWeapon = false;
                     }
                     else if (weaponselection == 3)
                     {
                         weapon3ammo -= 1;
                         canShootCurrentWeapon = false;
                     }
                     FireBullet();
                     currentweaponammo -= 1;
                     UpdateAmmoAndShootSpeed();
                     StartCoroutine("shootwait");
                 }
             }
             else { }
         }
     }
 
     public void OnLevelWasLoaded(int level)
     {
         StartCoroutine("waitthenupdateammo");
 
         if (player.player == 1) weapon = GameObject.FindGameObjectWithTag("ui/weapon/weapon1").GetComponent<Image>();
         if (player.player == 2) weapon = GameObject.FindGameObjectWithTag("ui/weapon/weapon2").GetComponent<Image>();
         if (player.player == 3) weapon = GameObject.FindGameObjectWithTag("ui/weapon/weapon3").GetComponent<Image>();
         if (player.player == 4) weapon = GameObject.FindGameObjectWithTag("ui/weapon/weapon4").GetComponent<Image>();
 
         GetRefs();
         GetRefsBarrel();
         WeaponSwapFunction();
         WeaponNames();
         WeaponSprites();
         UpdateAmmoAndShootSpeed();
     }
 
     IEnumerator waitthenupdateammo()
     {
         yield return new WaitForSeconds(0.1f);
         UpdateAmmoAndShootSpeed();
     }
 
     public void UpdateAmmoAndShootSpeed()
     {
         if (weaponselection == 1)
         {
             if (currentweapontype == "Pistol")
             {
                 shootspeed = 20;
                 maxweapon1ammo = maxpistolammo;
             }
         }
         else if (weaponselection == 2)
         {
             if (currentweapontype == "Pistol")
             {
                 shootspeed = 20;
                 maxcurrentweaponammo = maxpistolammo;
                 maxweapon2ammo = maxpistolammo;
             }
             if (currentweapontype == "Assault Rifle")
             {
                 shootspeed = 20;
                 maxcurrentweaponammo = maxassaultrifleammo;
                 maxweapon2ammo = maxassaultrifleammo;
             }
             if (currentweapontype == "Shotgun")
             {
                 shootspeed = 8;
                 maxcurrentweaponammo = maxshotgunammo;
                 maxweapon2ammo = maxshotgunammo;
             }
             if (currentweapontype == "Sniper")
             {
                 shootspeed = 40;
                 maxcurrentweaponammo = maxsniperammo;
                 maxweapon2ammo = maxsniperammo;
             }
             if (currentweapontype == "LMG")
             {
                 shootspeed = 15;
                 maxcurrentweaponammo = maxlmgammo;
                 maxweapon2ammo = maxlmgammo;
             }
             if (currentweapontype == "SMG")
             {
                 shootspeed = 30;
                 maxcurrentweaponammo = maxsmgammo;
                 maxweapon2ammo = maxsmgammo;
             }
             if (currentweapontype == "Rocket Launcher")
             {
                 shootspeed = 30;
                 maxcurrentweaponammo = maxrocketlauncherammo;
                 maxweapon2ammo = maxrocketlauncherammo;
             }
         }
         else if (weaponselection == 3)
         {
             if (currentweapontype == "Pistol")
             {
                 shootspeed = 20;
                 maxweapon3ammo = maxpistolammo;
             }
             if (currentweapontype == "Assault Rifle")
             {
                 shootspeed = 20;
                 maxweapon3ammo = maxassaultrifleammo;
             }
             if (currentweapontype == "Shotgun")
             {
                 shootspeed = 8;
                 maxweapon3ammo = maxassaultrifleammo;
             }
             if (currentweapontype == "Sniper")
             {
                 shootspeed = 40;
                 maxweapon3ammo = maxassaultrifleammo;
             }
             if (currentweapontype == "LMG")
             {
                 shootspeed = 15;
                 maxweapon3ammo = maxassaultrifleammo;
             }
             if (currentweapontype == "SMG")
             {
                 shootspeed = 30;
                 maxweapon3ammo = maxassaultrifleammo;
             }
             if (currentweapontype == "Rocket Launcher")
             {
                 shootspeed = 30;
                 maxweapon3ammo = maxassaultrifleammo;
             }
         }
         Scene currentScene = SceneManager.GetActiveScene();
         string sceneName = currentScene.name;
         if (sceneName != "charactercreation") ammo.text = currentweaponammo + "/" + currentweaponreserve;
     }
 
     public void GetPrefix()
     {
         currentPrefix1 = currentweaponprefix1;
         currentPrefix2 = currentweaponprefix2;
     }
 
     public void GetReloadSpeed()
     {
         if (grippy == true)
         {
             pistolwait = defpistolwait * gripwait;
             assaultriflewait = defassaultriflewait * gripwait;
             shotgunwait = defshotgunwait * gripwait;
             sniperwait = defsniperwait * gripwait;
             lmgwait = deflmgwait * gripwait;
             smgwait = defsmgwait * gripwait;
             rocketlauncherwait = defrocketlauncherwait * gripwait;
         }
         if (sticky == true)
         {
             pistolwait = defpistolwait * stickywait;
             assaultriflewait = defassaultriflewait * stickywait;
             shotgunwait = defshotgunwait * stickywait;
             sniperwait = defsniperwait * stickywait;
             lmgwait = deflmgwait * stickywait;
             rocketlauncherwait = defrocketlauncherwait * stickywait;
         }
 
         if (currentweapontype == "Pistol") currentweaponwait = pistolwait;
         else if (currentweapontype == "Assault Rifle") currentweaponwait = assaultriflewait;
         else if (currentweapontype == "Shotgun") currentweaponwait = shotgunwait;
         else if (currentweapontype == "Sniper") currentweaponwait = sniperwait;
         else if (currentweapontype == "LMG") currentweaponwait = lmgwait;
         else if (currentweapontype == "SMG") currentweaponwait = smgwait;
         else if (currentweapontype == "Rocket Launcher") currentweaponwait = rocketlauncherwait;
     }
 
     public void MaxAmmos()
     {
         if (weapon2type == "Pistol") maxweapon2ammo = maxpistolammo;
         if (weapon2type == "Assault Rifle") maxweapon2ammo = maxassaultrifleammo;
         if (weapon2type == "Shotgun") maxweapon2ammo = maxshotgunammo;
         if (weapon2type == "Sniper") maxweapon2ammo = maxsniperammo;
         if (weapon2type == "LMG") maxweapon2ammo = maxlmgammo;
         if (weapon2type == "SMG") maxweapon2ammo = maxsmgammo;
         if (weapon2type == "Rocket Launcher") maxweapon2ammo = maxrocketlauncherammo;
 
         if (weapon3type == "Pistol") maxweapon3ammo = maxpistolammo;
         if (weapon3type == "Assault Rifle") maxweapon3ammo = maxassaultrifleammo;
         if (weapon3type == "Shotgun") maxweapon3ammo = maxshotgunammo;
         if (weapon3type == "Sniper") maxweapon3ammo = maxshotgunammo;
         if (weapon3type == "LMG") maxweapon3ammo = maxlmgammo;
         if (weapon3type == "SMG") maxweapon3ammo = maxsmgammo;
         if (weapon3type == "Rocket Launcher") maxweapon3ammo = maxrocketlauncherammo;
     }
 
     public void ResetPrefix()
     {
         piercing = false;
         metal = false;
         grippy = false;
         bouncy = false;
         crippling = false;
         lightbool = false;
         legendary = false;
 
         paper = false;
         sticky = false;
         homemade = false;
         warped = false;
         broken = false;
         taxing = false;
         heavy = false;
 }
 
     public void Prefix1()
     {
         if (currentPrefix1 == "Piercing ") piercing = true;
         else if (currentPrefix1 == "Metal ") metal = true;
         else if (currentPrefix1 == "Grippy ") grippy = true;
         else if (currentPrefix1 == "Bouncy ") bouncy = true;
         else if (currentPrefix1 == "Crippling ") crippling = true;
         else if (currentPrefix1 == "Light ") lightbool = true;
         else if (currentPrefix1 == "LEGENDARY ") legendary = true;
         else if (currentPrefix1 == "Paper ") paper = true;
         else if (currentPrefix1 == "Sticky ") sticky = true;
         else if (currentPrefix1 == "Homemade ") homemade = true;
         else if (currentPrefix1 == "Warped ") warped = true;
         else if (currentPrefix1 == "Broken ") broken = true;
         else if (currentPrefix1 == "Taxing ") taxing = true;
         else if (currentPrefix1 == "Heavy ") heavy = true;
     }
 
     public void Prefix2()
     {
         if (currentPrefix2 == "Piercing ") piercing = true;
         else if (currentPrefix2 == "Metal ") metal = true;
         else if (currentPrefix2 == "Grippy ") grippy = true;
         else if (currentPrefix2 == "Bouncy ") bouncy = true;
         else if (currentPrefix2 == "Crippling ") crippling = true;
         else if (currentPrefix2 == "Light ") lightbool = true;
         else if (currentPrefix2 == "LEGENDARY ") legendary = true;
         else if (currentPrefix2 == "Paper ") paper = true;
         else if (currentPrefix2 == "Sticky ") sticky = true;
         else if (currentPrefix2 == "Homemade ") homemade = true;
         else if (currentPrefix2 == "Warped ") warped = true;
         else if (currentPrefix2 == "Broken ") broken = true;
         else if (currentPrefix2 == "Taxing ") taxing = true;
         else if (currentPrefix2 == "Heavy ") heavy = true;
     }
 
     public void CanShootWeapon()
     {
         if (canShootCurrentWeapon == true)
         {
             if (weaponselection == 1)
             {
                 canShootWeapon1 = true;
                 canShootWeapon2 = false;
                 canShootWeapon3 = false;
             }
             else if (weaponselection == 2)
             {
                 canShootWeapon2 = true;
                 canShootWeapon1 = false;
                 canShootWeapon3 = false;
             }
             else if (weaponselection == 3)
             {
                 canShootWeapon3 = true;
                 canShootWeapon1 = false;
                 canShootWeapon2 = false;
             }
         }
         else if (canShootCurrentWeapon == false)
         {
             canShootWeapon1 = false;
             canShootWeapon2 = false;
             canShootWeapon3 = false;
         }
     }
 
     public void GetRefsBarrel()
     {
         Scene currentScene = SceneManager.GetActiveScene();
         string sceneName = currentScene.name;
 
         if (sceneName != "charactercreation")
         {
             if (player.player == 1) 
             {
                 ammo = GameObject.FindGameObjectWithTag("ui/ammo/ammo1").GetComponent<Text>();
             }
             else if (player.player == 2)
             {
                 ammo = GameObject.FindGameObjectWithTag("ui/ammo/ammo2").GetComponent<Text>();
             }
             else if (player.player == 3)
             {
                 ammo = GameObject.FindGameObjectWithTag("ui/ammo/ammo3").GetComponent<Text>();
             }
             else if (player.player == 4)
             {
                 ammo = GameObject.FindGameObjectWithTag("ui/ammo/ammo4").GetComponent<Text>();
             }
         }
     }
 
     public IEnumerator reloadcheck()
     {
         if (weaponselection == 1) canShootWeapon1 = false;
         else if (weaponselection == 2) canShootWeapon2 = false;
         else if (weaponselection == 3) canShootWeapon3 = false;
 
         if (currentweapontype == "Pistol")
         {
             currentweaponwait = pistolwait;
             maxcurrentweaponammo = maxpistolammo;
         }
         else if (currentweapontype == "Assault Rifle")
         {
             currentweaponwait = assaultriflewait;
             maxcurrentweaponammo = maxassaultrifleammo;
         }
         else if (currentweapontype == "Shotgun")
         {
             currentweaponwait = shotgunwait;
             maxcurrentweaponammo = maxsniperammo;
         }
         else if (currentweapontype == "Sniper")
         {
             currentweaponwait = sniperwait;
             maxcurrentweaponammo = maxsniperammo;
         }
         else if (currentweapontype == "LMG")
         {
             currentweaponwait = lmgwait;
             maxcurrentweaponammo = maxlmgammo;
         }
         else if (currentweapontype == "SMG")
         {
             currentweaponwait = smgwait;
             maxcurrentweaponammo = maxsmgammo;
         }
         else if (currentweapontype == "Rocket Launcher")
         {
             currentweaponwait = rocketlauncherwait;
             maxcurrentweaponammo = maxrocketlauncherammo;
         }
         yield return new WaitForSeconds(currentweaponwait);
         currentweaponammo = maxcurrentweaponammo;
         UpdateAmmoAndShootSpeed();
         CanShootThisWeapon();
         GetAndSetMaxAmmo();
     }
 
     IEnumerator shootwait()
     {
         if (currentweapontype == "Pistol") currentweaponwait = pistolwait;
         else if (currentweapontype == "Assault Rifle") currentweaponwait = assaultriflewait;
         else if (currentweapontype == "Shotgun") currentweaponwait = shotgunwait;
         else if (currentweapontype == "Sniper") currentweaponwait = sniperwait;
         else if (currentweapontype == "LMG") currentweaponwait = lmgwait;
         else if (currentweapontype == "SMG") currentweaponwait = smgwait;
         else if (currentweapontype == "Rocket Launcher") currentweaponwait = rocketlauncherwait;
         if (currentweaponammo >= 1)
         {
             yield return new WaitForSeconds(currentweaponwait);
             CanShootThisWeapon();
         }
     }
 
     public void CallWeaponSwapFunctions()
     {
         player.PrefixStuff();
 
         WeaponNames(); 
         GetReloadSpeed(); 
         MaxAmmos(); 
         ResetPrefix(); 
         GetPrefix(); 
         Prefix1(); 
         Prefix2(); 
         WeaponSprites(); 
         UpdateAmmoAndShootSpeed();
     }
 
     private void FireBullet()
     {
         if (currentweapontype != "Rocket Launcher") 
         {
             if (keyboard == true)
             {
                 GameObject firedBullet = Instantiate(bullet, barrelTip.position, barrelTip.rotation);
                 firedBullet.GetComponent<Rigidbody2D>().velocity = barrelTip.up * shootspeed;
                 firedBullet.GetComponent<bullet>().playershot = player.player;
                 if (currentweapontype == "Shotgun")
                 {
                     GameObject firedBullet2 = Instantiate(bullet, barrelTip2.position, barrelTip2.rotation);
                     firedBullet2.GetComponent<Rigidbody2D>().velocity = barrelTip2.up * shootspeed;
                     firedBullet2.GetComponent<bullet>().playershot = player.player;
                     GameObject firedBullet3 = Instantiate(bullet, barrelTip3.position, barrelTip3.rotation);
                     firedBullet3.GetComponent<Rigidbody2D>().velocity = barrelTip3.up * shootspeed;
                     firedBullet3.GetComponent<bullet>().playershot = player.player;
                 }
             }
             else if (keyboard == false)
             {
                 GameObject firedBullet = Instantiate(bullet, barrelTip.position, barrelTip.rotation);
                 firedBullet.GetComponent<Rigidbody2D>().velocity = barrelTip.right * shootspeed;
                 firedBullet.GetComponent<bullet>().playershot = player.player;
                 if (currentweapontype == "Shotgun")
                 {
                     GameObject firedBullet2 = Instantiate(bullet, barrelTip2.position, barrelTip2.rotation);
                     firedBullet2.GetComponent<Rigidbody2D>().velocity = barrelTip2.right * shootspeed;
                     firedBullet2.GetComponent<bullet>().playershot = player.player;
                     GameObject firedBullet3 = Instantiate(bullet, barrelTip3.position, barrelTip3.rotation);
                     firedBullet3.GetComponent<Rigidbody2D>().velocity = barrelTip3.right * shootspeed;
                     firedBullet3.GetComponent<bullet>().playershot = player.player;
                 }
             }
         }
 
         else if (currentweapontype == "Rocket Launcher")
         {
             if (keyboard == true)
             {
                 GameObject firedRocket = Instantiate(rocket, barrelTip.position, barrelTip.rotation);
                 firedRocket.GetComponent<Rigidbody2D>().velocity = barrelTip.up * shootspeed;
                 firedRocket.GetComponent<bullet>().playershot = player.player;
             }
             else if (keyboard == false)
             {
                 GameObject firedRocket = Instantiate(rocket, barrelTip.position, barrelTip.rotation);
                 firedRocket.GetComponent<Rigidbody2D>().velocity = barrelTip.right * shootspeed;
                 firedRocket.GetComponent<bullet>().playershot = player.player;
             }
         }
     }
 }
 
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by streeetwalker · Apr 15, 2020 at 11:49 AM

@Cryno1000, I haven't followed through all of your code, but one thing is spotted was that you are calling WeaponSwapFunction recursively.

That practice certainly has the potential to cause the type if issue you experience, I'm not sure need to do any recursion in what it seems like that function is doing - you could handle the logic in a straightforward fashion.

Maybe that is not the problem, I don't know, but If I were you I would create a counter variable outside of WeaponSwapFunction and then at the very top of WeaponSwapFunction, add this code, for example:

    private int recursions = 1;
    public void WeaponSwapFunction() {
            Debug.Log( "recursions = " + recursions++ );  

And see how many times that is actually getting called every time you swap.

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 Cryno1000 · Apr 16, 2020 at 12:50 AM 0
Share

Thanks for your help, but I couldn't find anything wrong after using your code. But I did figure out that everything works fine UNTIL the player shoots the rocket launcher. The rocket launcher works fine but after they shoot it, shooting any weapon doesn't work. Any ideas?

avatar image streeetwalker Cryno1000 · Apr 16, 2020 at 03:55 AM 0
Share

OK, so the problem is not with Swap, but shooting?

What's the Wait variables for, so that you can only shoot at a specific rate of fire?

Perhaps you are applying the Wait before it shoots the first time, whereas what you really want to do is shoot the first time immediately and only wait in between shots?

It would seem if you are getting a delay, the problem is how and where you are handling your Wait variable.

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

805 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Having an issue where after I shoot a specific weapon, other weapons don't work as intended. 0 Answers

My script doesnt work? 0 Answers

Audio Source does not contain definition for any of the Play functions... 2 Answers

Health bar rotation on camera 0 Answers

How do I add a simple jump function to this script? 2 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