Advertisement
Guest User

Untitled

a guest
Jul 18th, 2019
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.84 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5.  
  6. public enum Firemode
  7. {
  8. SemiAuto,
  9. FullAuto
  10. }
  11.  
  12. public class Shotgun : MonoBehaviour
  13. {
  14. [Header("Put Animator Controller here")]
  15. public Animator animator;
  16.  
  17.  
  18.  
  19. private AudioSource audioSource;
  20. private bool fireLock = false;
  21.  
  22. [Header("Object References")]
  23. public ParticleSystem muzzleFlash;
  24. public Transform shootPoint;
  25. public GameObject bloodPrefab;
  26. public GameObject sparkPrefab;
  27. [Header("UI References")]
  28. public Text ammoText;
  29. [Header("Sound References")]
  30. public AudioClip fireSound;
  31. public AudioClip dryFireSound;
  32. public AudioClip reloadSound;
  33.  
  34. [Header("Weapon Attributes")]
  35. public Firemode fireMode = Firemode.FullAuto;
  36. public float damage = 10f;
  37. public float fireRate = 1.0f;
  38. public int bulletsInClip;
  39. public int clipSize = 30;
  40. public int bulletsLeft;
  41. public int maxAmmo = 120;
  42. public int pellets = 1;
  43. public float reloadTime = 2.978f;
  44. public float spread = 0.1f;
  45. private bool isReloading = false;
  46.  
  47.  
  48. void Start()
  49. {
  50. animator = GetComponent<Animator>();
  51. audioSource = GetComponent<AudioSource>();
  52.  
  53. bulletsInClip = clipSize;
  54. bulletsLeft = maxAmmo;
  55.  
  56. UpdateTexts();
  57. }
  58.  
  59. void UpdateTexts() {
  60. ammoText.text = "Ammo: " + bulletsInClip + " / " + bulletsLeft;
  61. }
  62.  
  63.  
  64. void Update()
  65. {
  66. if (isReloading)
  67. return;
  68. if (fireMode == Firemode.FullAuto && Input.GetButton("Fire1"))
  69. {
  70. CheckFire();
  71. }
  72. else if (fireMode == Firemode.SemiAuto && Input.GetButtonDown("Fire1"))
  73. {
  74. CheckFire();
  75. }
  76.  
  77. if (Input.GetButtonDown("Reload"))
  78. {
  79. CheckReload();
  80. }
  81. }
  82.  
  83. void CheckFire()
  84. { if (fireLock) return;
  85.  
  86. if (bulletsInClip > 0)
  87. {
  88. if (isReloading == false){
  89. Fire();
  90. animator.CrossFadeInFixedTime("Fire", 0.01f);
  91. }
  92. else
  93. {
  94. DryFire();
  95. }
  96. }
  97. }
  98.  
  99. void Fire()
  100. {
  101. if (bulletsInClip >=(5 + bulletsToLoad)){
  102. bulletsInClip = bulletsToLoad
  103. }
  104.  
  105.  
  106. bulletsInClip--;
  107.  
  108. audioSource.PlayOneShot(fireSound);
  109. fireLock = true;
  110.  
  111. for(int i = 0; i < pellets; i++) {
  112. DetectHit();
  113. }
  114.  
  115. muzzleFlash.Stop();
  116. muzzleFlash.Play();
  117.  
  118. UpdateTexts();
  119.  
  120. StartCoroutine(CoResetFireLock());
  121. }
  122.  
  123. public void CreateBlood(Vector3 pos, Quaternion rot)
  124. {
  125. GameObject blood = Instantiate(bloodPrefab, pos, rot);
  126. Destroy(blood, 1f);
  127. }
  128.  
  129. void DetectHit() {
  130. RaycastHit hit;
  131.  
  132. if(Physics.Raycast(shootPoint.position, CalculateSpread(spread, shootPoint), out hit)) {
  133. if (hit.transform.CompareTag("Enemy")) {
  134. Health health = hit.transform.GetComponent<Health>();
  135.  
  136. if(health == null) {
  137. throw new System.Exception("Cannot found Health Component on Enemy.");
  138. }
  139. else {
  140. health.TakeDamage(damage);
  141. CreateBlood(hit.point, hit.transform.rotation);
  142. }
  143. }
  144. else
  145. {
  146. GameObject spark = Instantiate(sparkPrefab, hit.point, hit.transform.rotation);
  147. Destroy(spark, 1);
  148. }
  149. }
  150. }
  151.  
  152. Vector3 CalculateSpread(float spread, Transform shootPoint)
  153. {
  154. return Vector3.Lerp(shootPoint.TransformDirection(Vector3.forward * 100), Random.onUnitSphere, spread);
  155. }
  156.  
  157. void DryFire()
  158. {
  159. audioSource.PlayOneShot(dryFireSound);
  160. //fireLock = true;
  161.  
  162. //StartCoroutine(CoResetFireLock());
  163. }
  164.  
  165. IEnumerator CoResetFireLock()
  166. {
  167. yield return new WaitForSeconds(fireRate);
  168. fireLock = false;
  169. }
  170.  
  171. void CheckReload()
  172. {
  173. if (bulletsLeft > 0 && bulletsInClip < clipSize)
  174. {
  175. if (fireLock == false){
  176. StartCoroutine(Reload());
  177. audioSource.PlayOneShot(reloadSound);
  178. }}
  179. }
  180.  
  181. IEnumerator Reload()
  182. {animator.CrossFadeInFixedTime("ReloadStartEmpty", 0.01f);
  183. isReloading = true;
  184.  
  185. yield return new WaitForSeconds(reloadTime);
  186.  
  187. int bulletsToLoad = clipSize - bulletsInClip;
  188. int bulletsToSub = (bulletsLeft >= bulletsToLoad) ? bulletsToLoad : bulletsLeft;
  189.  
  190. bulletsLeft -= bulletsToSub;
  191. bulletsInClip += bulletsToLoad;
  192.  
  193. isReloading = false;
  194.  
  195. UpdateTexts();
  196. }
  197.  
  198. public void OnReload()
  199. {
  200.  
  201. }
  202. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement