Advertisement
Guest User

Untitled

a guest
Oct 24th, 2017
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.51 KB | None | 0 0
  1. using UnityEngine;
  2. using UnityEngine.UI;
  3. using System.Collections;
  4.  
  5. public class RaycastShoot : MonoBehaviour
  6. {
  7. public float damage = 10f;
  8. public float range = 100f;
  9. public float impactForce = 30f;
  10. public float fireRate = 15f;
  11. private bool isReloading = false;
  12. public Animator animator;
  13. public Text currentammoText;
  14. public Text leftammoText;
  15. private bool isActive = true;
  16.  
  17. private int maxAmmo = 8;
  18. private int leftAmmo;
  19. private int currentAmmo;
  20.  
  21. public ParticleSystem muzzleFlash;
  22. public GameObject impactEffect;
  23. public ParticleSystem stuckFlash;
  24. public float reloadTime = 1.5f;
  25. private float nextTimeToFire = 0f;
  26.  
  27. public Camera fpsCam;
  28.  
  29. Vector3 recoilSmoothDampVelocity;
  30.  
  31. void Start()
  32. {
  33. currentAmmo = maxAmmo;
  34. leftAmmo = 64;
  35. currentAmmo = 8;
  36. }
  37.  
  38. void Update()
  39. {
  40. if (isReloading)
  41. return;
  42.  
  43. if (currentAmmo <= 0)
  44. {
  45. StartCoroutine(Reload());
  46. return;
  47. }
  48.  
  49. if (Input.GetButtonDown("Fire1") && Time.time >= nextTimeToFire)
  50. {
  51. nextTimeToFire = Time.time + 1f / fireRate;
  52. Shoot();
  53. }
  54. transform.localPosition = Vector3.SmoothDamp(transform.localPosition, Vector3.zero, ref recoilSmoothDampVelocity, .1f);
  55. }
  56.  
  57. void OnEnable()
  58. {
  59. isReloading = false;
  60. animator.SetBool("Reloading", false);
  61. }
  62.  
  63. IEnumerator Reload()
  64. {
  65. if (isActive == true)
  66. {
  67. isReloading = true;
  68. Debug.Log("Reloading...");
  69. FindObjectOfType<AudioManager>().Play("Reload1");
  70.  
  71. leftAmmo = leftAmmo - 8;
  72. leftammoText.text = "" + leftAmmo.ToString();
  73. currentAmmo = currentAmmo + 8;
  74. currentammoText.text = "" + currentAmmo.ToString();
  75.  
  76. animator.SetBool("Reloading", true);
  77.  
  78. yield return new WaitForSeconds(reloadTime - .25f);
  79. animator.SetBool("Reloading", false);
  80. yield return new WaitForSeconds(.25f);
  81.  
  82. currentAmmo = maxAmmo;
  83. isReloading = false;
  84. }
  85. }
  86.  
  87. void Shoot()
  88. {
  89. if (isActive == true)
  90. {
  91. muzzleFlash.Play();
  92.  
  93. currentAmmo = currentAmmo - 1;
  94. currentammoText.text = "" + currentAmmo.ToString();
  95.  
  96. FindObjectOfType<AudioManager>().Play("GunShot1");
  97.  
  98. RaycastHit hit;
  99. if (Physics.Raycast(fpsCam.transform.position, fpsCam.transform.forward, out hit, range))
  100. {
  101. Debug.Log(hit.transform.name);
  102.  
  103. ShootableBox target = hit.transform.GetComponent<ShootableBox>();
  104. if (target != null)
  105. {
  106. target.TakeDamage(damage);
  107. }
  108.  
  109. if (hit.rigidbody != null)
  110. {
  111. hit.rigidbody.AddForce(-hit.normal * impactForce);
  112. }
  113.  
  114. transform.localPosition -= Vector3.forward * .2f;
  115.  
  116. GameObject impactGO = Instantiate(impactEffect, hit.point, Quaternion.LookRotation(hit.normal));
  117. Destroy(impactGO, 2f);
  118. }
  119.  
  120. }
  121.  
  122. if (currentAmmo == 0 && leftAmmo == 0)
  123. {
  124. isActive = false;
  125. }
  126.  
  127. if(Input.GetButtonDown("Fire1") && isActive == false)
  128. {
  129. FindObjectOfType<AudioManager>().Play("Stuck1");
  130. }
  131. }
  132. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement