Advertisement
Guest User

Untitled

a guest
Apr 23rd, 2021
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.17 KB | None | 0 0
  1. using UnityEngine;
  2. using TMPro;
  3.  
  4. public class GunSystem : MonoBehaviour
  5. {
  6.  
  7. //Animations
  8.  
  9. //Gun stats
  10. public int damage;
  11. public float timeBetweenShooting, spread, range, reloadTime, timeBetweenShots;
  12. public int magazineSize, bulletsPerTap;
  13. public bool allowButtonHold;
  14. int bulletsLeft, bulletsShot;
  15.  
  16. //bools
  17. bool shooting, readyToShoot, reloading;
  18.  
  19. //Reference
  20. public Camera fpsCam;
  21. public Transform attackPoint;
  22. public RaycastHit rayHit;
  23. public LayerMask whatIsEnemy;
  24.  
  25.  
  26. //Graphics
  27. public GameObject muzzleFlash, bulletHoleGraphic;
  28. public float camShakeMagnitude, camShakeDuration;
  29. public TextMeshProUGUI text;
  30.  
  31. private void Awake()
  32. {
  33. bulletsLeft = magazineSize;
  34. readyToShoot = true;
  35.  
  36. }
  37. private void Update()
  38. {
  39. MyInput();
  40.  
  41. //SetText
  42. text.SetText(bulletsLeft + " / " + magazineSize);
  43. }
  44. private void MyInput()
  45. {
  46. if (allowButtonHold) shooting = Input.GetKey(KeyCode.Mouse0);
  47. else shooting = Input.GetKeyDown(KeyCode.Mouse0);
  48.  
  49. if (Input.GetKeyDown(KeyCode.R) && bulletsLeft < magazineSize && !reloading) Reload();
  50.  
  51. //Shoot
  52. if (readyToShoot && shooting && !reloading && bulletsLeft > 0)
  53. {
  54. bulletsShot = bulletsPerTap;
  55. Shoot();
  56. }
  57. }
  58. private void Shoot()
  59. {
  60. readyToShoot = false;
  61.  
  62. //Spread
  63. float x = Random.Range(-spread, spread);
  64. float y = Random.Range(-spread, spread);
  65.  
  66. //Calculate Direction with Spread
  67. Vector3 direction = fpsCam.transform.forward + new Vector3(x, y, 0);
  68.  
  69. //Play Animation
  70.  
  71.  
  72. //RayCast
  73. if (Physics.Raycast(fpsCam.transform.position, direction, out rayHit, range, whatIsEnemy))
  74. {
  75. Debug.Log(rayHit.collider.name);
  76. TriggerAppear platform = rayHit.collider.gameObject.GetComponent<TriggerAppear>();
  77. AIHealth health = rayHit.collider.gameObject.GetComponent<AIHealth>();
  78. if(platform != null)
  79. {
  80. platform.Platform.SetActive(true);
  81. }
  82. if(rayHit.collider.gameObject.tag == "Enemy")
  83. {
  84. if(health != null)
  85. {
  86. health.Health -= damage;
  87. }
  88. }
  89. }
  90.  
  91. //ShakeCamera
  92.  
  93. //Graphics
  94. Instantiate(bulletHoleGraphic, rayHit.point + rayHit.normal * 0.002f, Quaternion.LookRotation(rayHit.normal), rayHit.collider.gameObject.transform);
  95. Instantiate(muzzleFlash, attackPoint.position, Quaternion.identity);
  96.  
  97. bulletsLeft--;
  98. bulletsShot--;
  99.  
  100. Invoke("ResetShot", timeBetweenShooting);
  101.  
  102. if (bulletsShot > 0 && bulletsLeft > 0)
  103. Invoke("Shoot", timeBetweenShots);
  104. }
  105. private void ResetShot()
  106. {
  107. readyToShoot = true;
  108. }
  109. private void Reload()
  110. {
  111. reloading = true;
  112. Invoke("ReloadFinished", reloadTime);
  113. }
  114. private void ReloadFinished()
  115. {
  116. bulletsLeft = magazineSize;
  117. reloading = false;
  118. }
  119. }
  120.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement