[csharp] canShoot issue
Viewer
*** This page was generated with the meta tag "noindex, nofollow". This happened because you selected this option before saving or the system detected it as spam. This means that this page will never get into the search engines and the search bot will not crawl it. There is nothing to worry about, you can still share it with anyone.
- SHOTGUN SCRIPT BELOW:
- using System.Collections;
- using System.Collections.Generic;
- using TMPro;
- using UnityEngine;
- public class ShotgunWeapon : MonoBehaviour
- {
- [SerializeField] Camera FPCamera;
- [SerializeField] float range = 100f;
- [SerializeField] float damage;
- [SerializeField] ParticleSystem fireFlash;
- [SerializeField] GameObject hitEffect;
- [SerializeField] AudioSource hitSound;
- [SerializeField] AudioSource shotgunRechargeSound;
- [SerializeField] Ammo ammoSlot;
- [SerializeField] AmmoType ammoType;
- [SerializeField] AudioSource ammoRechargeSFX;
- [SerializeField] AudioSource gunBlankSFX;
- [SerializeField] ParticleSystem ammoRegentParticles;
- [SerializeField] float timeBetweenShots = 0.5f;
- [SerializeField] TextMeshProUGUI ammoText;
- bool canShoot = true;
- private void OnEnable()
- {
- canShoot = true;
- }
- static Animator anim;
- private void Start()
- {
- anim = GetComponent<Animator>();
- }
- void Update()
- {
- DisplayAmmo();
- if (Input.GetButtonDown("Fire1") && canShoot == true)
- {
- anim.SetBool("fire", true);
- StartCoroutine(Shoot());
- }
- else anim.SetBool("fire", false);
- if (Input.GetKeyDown(KeyCode.R) && ammoSlot.GetCurrentAmmo(ammoType) < 20)
- {
- ammoRegentParticles.Play();
- ammoRechargeSFX.Play();
- StartCoroutine(RegenAmmo());
- }
- if (PauseMenu.GameIsPaused)
- {
- canShoot = false;
- }
- else canShoot = true;
- if (DeathHandler.isGameOverPaused)
- {
- canShoot = false;
- }
- }
- IEnumerator RegenAmmo()
- {
- if (ammoSlot.GetCurrentAmmo(ammoType) < 10)
- {
- ammoSlot.RegenerateAmmo(ammoType);
- }
- yield return new WaitForSeconds(3);
- }
- private void DisplayAmmo()
- {
- int currentAmmo = ammoSlot.GetCurrentAmmo(ammoType);
- ammoText.text = currentAmmo.ToString();
- }
- IEnumerator Shoot()
- {
- canShoot = false;
- if (ammoSlot.GetCurrentAmmo(ammoType) > 0)
- {
- PlayFireFlash();
- ProcessRaycast();
- ammoSlot.ReduceCurrentAmmo(ammoType);
- }
- else
- {
- gunBlankSFX.Play();
- yield return new WaitForSeconds(timeBetweenShots);
- canShoot = true;
- }
- }
- private void PlayFireFlash()
- {
- fireFlash.Play();
- }
- private void ProcessRaycast()
- {
- RaycastHit hit;
- if (Physics.Raycast(FPCamera.transform.position, FPCamera.transform.forward, out hit, range))
- {
- IDamageable target = hit.transform.GetComponent<IDamageable>();
- if (target != null)
- target.TakeDamage(damage);
- CreateHitImpact(hit);
- hitSound.Play();
- shotgunRechargeSound.Play();
- }
- else
- {
- hitSound.Play();
- shotgunRechargeSound.Play();
- return;
- }
- }
- private void CreateHitImpact(RaycastHit hit)
- {
- GameObject impact = Instantiate(hitEffect, hit.point, Quaternion.identity);
- Destroy(impact, .4f);
- }
- }
- PAUSE MENU SCRIPT BELOW:
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using UnityEngine.SceneManagement;
- public class PauseMenu : MonoBehaviour
- {
- public static bool GameIsPaused = false;
- [SerializeField] GameObject pauseMenuUI;
- [SerializeField] GameObject optionsUI;
- [SerializeField] GameObject controlsUI;
- Weapon weapon;
- private void Start()
- {
- weapon = FindObjectOfType<Weapon>();
- }
- void Update()
- {
- if (Input.GetKeyDown(KeyCode.Escape))
- {
- if (GameIsPaused)
- {
- Resume();
- }
- else
- {
- Pause();
- }
- }
- }
- public void Resume()
- {
- pauseMenuUI.SetActive(false);
- optionsUI.SetActive(false);
- controlsUI.SetActive(false);
- Time.timeScale = 1f;
- GameIsPaused = false;
- FindObjectOfType<WeaponSwitcher>().enabled = true;
- //FindObjectOfType<Weapon>().enabled = true;
- //FindObjectOfType<AutoArmWeapon>().enabled = true;
- //FindObjectOfType<ShotgunWeapon>().enabled = true;
- // if (weapon == true)
- //{
- // FindObjectOfType<WeaponZoom>().enabled = true;
- // }
- Cursor.lockState = CursorLockMode.Locked;
- Cursor.visible = false;
- }
- void Pause()
- {
- pauseMenuUI.SetActive(true);
- Time.timeScale = 0f;
- GameIsPaused = true;
- FindObjectOfType<WeaponSwitcher>().enabled = false;
- //FindObjectOfType<Weapon>().enabled = false;
- //FindObjectOfType<AutoArmWeapon>().enabled = false;
- //FindObjectOfType<ShotgunWeapon>().enabled = false;
- //if (weapon == true)
- // {
- // FindObjectOfType<WeaponZoom>().enabled = false;
- //}
- Cursor.lockState = CursorLockMode.None;
- Cursor.visible = true;
- }
- public void LoadMainMenu()
- {
- SceneManager.LoadScene(0);
- }
- public void QuitGame()
- {
- Application.Quit();
- }
- }
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: