[csharp] canShoot issue

Viewer

copydownloadembedprintName: canShoot issue
  1.  SHOTGUN SCRIPT BELOW:
  2.  
  3.  
  4. using System.Collections;
  5. using System.Collections.Generic;
  6. using TMPro;
  7. using UnityEngine;
  8.  
  9. public class ShotgunWeapon : MonoBehaviour
  10. {
  11.  
  12.     [SerializeField] Camera FPCamera;
  13.     [SerializeField] float range = 100f;
  14.     [SerializeField] float damage;
  15.     [SerializeField] ParticleSystem fireFlash;
  16.     [SerializeField] GameObject hitEffect;
  17.     [SerializeField] AudioSource hitSound;
  18.     [SerializeField] AudioSource shotgunRechargeSound;
  19.     [SerializeField] Ammo ammoSlot;
  20.     [SerializeField] AmmoType ammoType;
  21.  
  22.     [SerializeField] AudioSource ammoRechargeSFX;
  23.     [SerializeField] AudioSource gunBlankSFX;
  24.     [SerializeField] ParticleSystem ammoRegentParticles;
  25.  
  26.     [SerializeField] float timeBetweenShots = 0.5f;
  27.  
  28.     [SerializeField] TextMeshProUGUI ammoText;
  29.  
  30.     bool canShoot = true;
  31.  
  32.     private void OnEnable()
  33.     {
  34.         canShoot = true;
  35.         
  36.     }
  37.  
  38.     static Animator anim;
  39.  
  40.     private void Start()
  41.     {
  42.         anim = GetComponent<Animator>();
  43.  
  44.     }
  45.     void Update()
  46.     {
  47.         DisplayAmmo();
  48.  
  49.         if (Input.GetButtonDown("Fire1") && canShoot == true)
  50.         {
  51.             anim.SetBool("fire"true);
  52.             StartCoroutine(Shoot());
  53.         }
  54.         else anim.SetBool("fire"false);
  55.  
  56.         if (Input.GetKeyDown(KeyCode.R) && ammoSlot.GetCurrentAmmo(ammoType) < 20)
  57.         {
  58.             ammoRegentParticles.Play();
  59.             ammoRechargeSFX.Play();
  60.             StartCoroutine(RegenAmmo());
  61.         }
  62.  
  63.         if (PauseMenu.GameIsPaused)
  64.         {
  65.             canShoot = false;
  66.         }
  67.         else canShoot = true;
  68.  
  69.  
  70.         if (DeathHandler.isGameOverPaused)
  71.         {
  72.             canShoot = false;
  73.         }
  74.  
  75.     }
  76.  
  77.     
  78.  
  79.     IEnumerator RegenAmmo()
  80.     {
  81.         if (ammoSlot.GetCurrentAmmo(ammoType) < 10)
  82.         {
  83.             
  84.             ammoSlot.RegenerateAmmo(ammoType);
  85.         }
  86.         yield return new WaitForSeconds(3);
  87.  
  88.  
  89.  
  90.     }
  91.  
  92.     private void DisplayAmmo()
  93.     {
  94.         int currentAmmo = ammoSlot.GetCurrentAmmo(ammoType);
  95.         ammoText.text = currentAmmo.ToString();
  96.     }
  97.  
  98.     IEnumerator Shoot()
  99.     {
  100.  
  101.         canShoot = false;
  102.         if (ammoSlot.GetCurrentAmmo(ammoType) > 0)
  103.         {
  104.             
  105.             PlayFireFlash();
  106.             ProcessRaycast();
  107.             ammoSlot.ReduceCurrentAmmo(ammoType);
  108.  
  109.         }
  110.         else
  111.         {
  112.             gunBlankSFX.Play();
  113.             yield return new WaitForSeconds(timeBetweenShots);
  114.             canShoot = true;
  115.         }
  116.         
  117.         
  118.  
  119.     }
  120.  
  121.     private void PlayFireFlash()
  122.     {
  123.  
  124.         fireFlash.Play();
  125.     }
  126.  
  127.     private void ProcessRaycast()
  128.     {
  129.         RaycastHit hit;
  130.         if (Physics.Raycast(FPCamera.transform.position, FPCamera.transform.forwardout hit, range))
  131.         {
  132.  
  133.             IDamageable target = hit.transform.GetComponent<IDamageable>();
  134.  
  135.             if (target != null)
  136.                 target.TakeDamage(damage);
  137.             CreateHitImpact(hit);
  138.             hitSound.Play();
  139.             shotgunRechargeSound.Play();
  140.  
  141.         }
  142.         else
  143.         {
  144.             hitSound.Play();
  145.             shotgunRechargeSound.Play();
  146.             return;
  147.         }
  148.     }
  149.  
  150.     private void CreateHitImpact(RaycastHit hit)
  151.     {
  152.         GameObject impact = Instantiate(hitEffect, hit.point, Quaternion.identity);
  153.         Destroy(impact, .4f);
  154.  
  155.     }
  156. }
  157.  
  158.  
  159.  
  160.  
  161.  
  162. PAUSE MENU SCRIPT BELOW:
  163.  
  164. using System.Collections;
  165. using System.Collections.Generic;
  166. using UnityEngine;
  167. using UnityEngine.SceneManagement;
  168.  
  169. public class PauseMenu : MonoBehaviour
  170. {
  171.     public static bool GameIsPaused = false;
  172.     
  173.  
  174.     [SerializeField] GameObject pauseMenuUI;
  175.     [SerializeField] GameObject optionsUI;
  176.     [SerializeField] GameObject controlsUI;
  177.  
  178.     Weapon weapon;
  179.  
  180.     private void Start()
  181.     {
  182.         weapon = FindObjectOfType<Weapon>();
  183.     }
  184.     void Update()
  185.     {
  186.         
  187.         if (Input.GetKeyDown(KeyCode.Escape))
  188.         {
  189.             
  190.             if (GameIsPaused)
  191.             {
  192.                 Resume();
  193.  
  194.             }
  195.             else
  196.             {
  197.                 Pause();
  198.                
  199.             }
  200.         }
  201.     }
  202.  
  203.     public void Resume()
  204.     {
  205.         pauseMenuUI.SetActive(false);
  206.         optionsUI.SetActive(false);
  207.         controlsUI.SetActive(false);
  208.         Time.timeScale = 1f;
  209.         GameIsPaused = false;
  210.         FindObjectOfType<WeaponSwitcher>().enabled = true;
  211.         //FindObjectOfType<Weapon>().enabled = true;
  212.         //FindObjectOfType<AutoArmWeapon>().enabled = true;
  213.         //FindObjectOfType<ShotgunWeapon>().enabled = true;
  214.         
  215.        // if (weapon == true)
  216.        //{
  217.        //     FindObjectOfType<WeaponZoom>().enabled = true;
  218.        // }
  219.  
  220.  
  221.         Cursor.lockState = CursorLockMode.Locked;
  222.         Cursor.visible = false;
  223.  
  224.         
  225.     }
  226.  
  227.     void Pause()
  228.     {
  229.         pauseMenuUI.SetActive(true);
  230.         Time.timeScale = 0f;
  231.         GameIsPaused = true;
  232.         FindObjectOfType<WeaponSwitcher>().enabled = false;
  233.         //FindObjectOfType<Weapon>().enabled = false;
  234.         //FindObjectOfType<AutoArmWeapon>().enabled = false;
  235.         //FindObjectOfType<ShotgunWeapon>().enabled = false;
  236.        //if (weapon == true)
  237.        // {
  238.        //     FindObjectOfType<WeaponZoom>().enabled = false;
  239.        //}
  240.         
  241.  
  242.  
  243.         Cursor.lockState = CursorLockMode.None;
  244.         Cursor.visible = true;
  245.  
  246.         
  247.     }
  248.  
  249.     public void LoadMainMenu()
  250.     {
  251.         SceneManager.LoadScene(0);
  252.     }
  253.  
  254.     public void QuitGame()
  255.     {
  256.         Application.Quit();
  257.     }
  258. }
  259.  
  260.  
  261.  
  262.  
  263.  
  264.  
  265.  
  266.  
  267.  

Editor

You can edit this paste and save as new:


File Description
  • canShoot issue
  • Paste Code
  • 09 Dec-2020
  • 5.62 Kb
You can Share it: