Advertisement
Guest User

Untitled

a guest
Jul 18th, 2019
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.80 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. if (bulletsInClip == 0)
  82. {
  83. StartCoroutine(Reload());
  84. audioSource.PlayOneShot(reloadSound);
  85. }
  86. }
  87.  
  88. void CheckFire()
  89. { if (fireLock) return;
  90.  
  91. if (bulletsInClip > 0)
  92. {
  93.  
  94. Fire();
  95. animator.CrossFadeInFixedTime("Fire", 0.01f);
  96. }
  97. else
  98. {
  99. DryFire();
  100. }
  101. }
  102.  
  103. void Fire()
  104. {
  105. bulletsInClip--;
  106.  
  107. audioSource.PlayOneShot(fireSound);
  108. fireLock = true;
  109.  
  110. for(int i = 0; i < pellets; i++) {
  111. DetectHit();
  112. }
  113.  
  114. muzzleFlash.Stop();
  115. muzzleFlash.Play();
  116.  
  117. UpdateTexts();
  118.  
  119. StartCoroutine(CoResetFireLock());
  120. }
  121.  
  122. public void CreateBlood(Vector3 pos, Quaternion rot)
  123. {
  124. GameObject blood = Instantiate(bloodPrefab, pos, rot);
  125. Destroy(blood, 1f);
  126. }
  127.  
  128. void DetectHit() {
  129. RaycastHit hit;
  130.  
  131. if(Physics.Raycast(shootPoint.position, CalculateSpread(spread, shootPoint), out hit)) {
  132. if (hit.transform.CompareTag("Enemy")) {
  133. Health health = hit.transform.GetComponent<Health>();
  134.  
  135. if(health == null) {
  136. throw new System.Exception("Cannot found Health Component on Enemy.");
  137. }
  138. else {
  139. health.TakeDamage(damage);
  140. CreateBlood(hit.point, hit.transform.rotation);
  141. }
  142. }
  143. else
  144. {
  145. GameObject spark = Instantiate(sparkPrefab, hit.point, hit.transform.rotation);
  146. Destroy(spark, 1);
  147. }
  148. }
  149. }
  150.  
  151. Vector3 CalculateSpread(float spread, Transform shootPoint)
  152. {
  153. return Vector3.Lerp(shootPoint.TransformDirection(Vector3.forward * 100), Random.onUnitSphere, spread);
  154. }
  155.  
  156. void DryFire()
  157. {
  158. audioSource.PlayOneShot(dryFireSound);
  159. fireLock = true;
  160.  
  161. StartCoroutine(CoResetFireLock());
  162. }
  163.  
  164. IEnumerator CoResetFireLock()
  165. {
  166. yield return new WaitForSeconds(fireRate);
  167. fireLock = false;
  168. }
  169.  
  170. void CheckReload()
  171. {
  172. if (bulletsLeft > 0 && bulletsInClip < clipSize)
  173. {
  174. StartCoroutine(Reload());
  175. audioSource.PlayOneShot(reloadSound);
  176. }
  177. }
  178.  
  179. IEnumerator Reload()
  180. {animator.CrossFadeInFixedTime("ReloadStartEmpty", 0.01f);
  181. isReloading = true;
  182.  
  183. yield return new WaitForSeconds(reloadTime);
  184.  
  185. int bulletsToLoad = clipSize - bulletsInClip;
  186. int bulletsToSub = (bulletsLeft >= bulletsToLoad) ? bulletsToLoad : bulletsLeft;
  187.  
  188. bulletsLeft -= bulletsToSub;
  189. bulletsInClip += bulletsToLoad;
  190.  
  191. isReloading = false;
  192.  
  193. UpdateTexts();
  194. }
  195.  
  196. public void OnReload()
  197. {
  198.  
  199. }
  200. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement