Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using UnityEngine.UI;
- public enum Firemode
- {
- SemiAuto,
- FullAuto
- }
- public class Shotgun : MonoBehaviour
- {
- [Header("Put Animator Controller here")]
- public Animator animator;
- private AudioSource audioSource;
- private bool fireLock = false;
- [Header("Object References")]
- public ParticleSystem muzzleFlash;
- public Transform shootPoint;
- public GameObject bloodPrefab;
- public GameObject sparkPrefab;
- [Header("UI References")]
- public Text ammoText;
- [Header("Sound References")]
- public AudioClip fireSound;
- public AudioClip dryFireSound;
- public AudioClip reloadSound;
- [Header("Weapon Attributes")]
- public Firemode fireMode = Firemode.FullAuto;
- public float damage = 10f;
- public float fireRate = 1.0f;
- public int bulletsInClip;
- public int clipSize = 30;
- public int bulletsLeft;
- public int maxAmmo = 120;
- public int pellets = 1;
- public float reloadTime = 2.978f;
- public float spread = 0.1f;
- private bool isReloading = false;
- void Start()
- {
- animator = GetComponent<Animator>();
- audioSource = GetComponent<AudioSource>();
- bulletsInClip = clipSize;
- bulletsLeft = maxAmmo;
- UpdateTexts();
- }
- void UpdateTexts() {
- ammoText.text = "Ammo: " + bulletsInClip + " / " + bulletsLeft;
- }
- void Update()
- {
- if (isReloading)
- return;
- if (fireMode == Firemode.FullAuto && Input.GetButton("Fire1"))
- {
- CheckFire();
- }
- else if (fireMode == Firemode.SemiAuto && Input.GetButtonDown("Fire1"))
- {
- CheckFire();
- }
- if (Input.GetButtonDown("Reload"))
- {
- CheckReload();
- }
- }
- void CheckFire()
- { if (fireLock) return;
- if (bulletsInClip > 0)
- {
- if (isReloading == false){
- Fire();
- animator.CrossFadeInFixedTime("Fire", 0.01f);
- }
- else
- {
- DryFire();
- }
- }
- }
- void Fire()
- {
- if (bulletsInClip >=(5 + bulletsToLoad)){
- bulletsInClip = bulletsToLoad
- }
- bulletsInClip--;
- audioSource.PlayOneShot(fireSound);
- fireLock = true;
- for(int i = 0; i < pellets; i++) {
- DetectHit();
- }
- muzzleFlash.Stop();
- muzzleFlash.Play();
- UpdateTexts();
- StartCoroutine(CoResetFireLock());
- }
- public void CreateBlood(Vector3 pos, Quaternion rot)
- {
- GameObject blood = Instantiate(bloodPrefab, pos, rot);
- Destroy(blood, 1f);
- }
- void DetectHit() {
- RaycastHit hit;
- if(Physics.Raycast(shootPoint.position, CalculateSpread(spread, shootPoint), out hit)) {
- if (hit.transform.CompareTag("Enemy")) {
- Health health = hit.transform.GetComponent<Health>();
- if(health == null) {
- throw new System.Exception("Cannot found Health Component on Enemy.");
- }
- else {
- health.TakeDamage(damage);
- CreateBlood(hit.point, hit.transform.rotation);
- }
- }
- else
- {
- GameObject spark = Instantiate(sparkPrefab, hit.point, hit.transform.rotation);
- Destroy(spark, 1);
- }
- }
- }
- Vector3 CalculateSpread(float spread, Transform shootPoint)
- {
- return Vector3.Lerp(shootPoint.TransformDirection(Vector3.forward * 100), Random.onUnitSphere, spread);
- }
- void DryFire()
- {
- audioSource.PlayOneShot(dryFireSound);
- //fireLock = true;
- //StartCoroutine(CoResetFireLock());
- }
- IEnumerator CoResetFireLock()
- {
- yield return new WaitForSeconds(fireRate);
- fireLock = false;
- }
- void CheckReload()
- {
- if (bulletsLeft > 0 && bulletsInClip < clipSize)
- {
- if (fireLock == false){
- StartCoroutine(Reload());
- audioSource.PlayOneShot(reloadSound);
- }}
- }
- IEnumerator Reload()
- {animator.CrossFadeInFixedTime("ReloadStartEmpty", 0.01f);
- isReloading = true;
- yield return new WaitForSeconds(reloadTime);
- int bulletsToLoad = clipSize - bulletsInClip;
- int bulletsToSub = (bulletsLeft >= bulletsToLoad) ? bulletsToLoad : bulletsLeft;
- bulletsLeft -= bulletsToSub;
- bulletsInClip += bulletsToLoad;
- isReloading = false;
- UpdateTexts();
- }
- public void OnReload()
- {
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement