Advertisement
romi_fauzi

AutoFire.cs

Sep 4th, 2018
286
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.72 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class AutoShoot : MonoBehaviour
  6. {
  7. public ShootProfile shootProfile;
  8. public GameObject bulletPrefabs;
  9. public Transform firePoint;
  10.  
  11. private float totalSpread;
  12. private WaitForSeconds rate, interval;
  13.  
  14. // Use this for initialization
  15. void OnEnable ()
  16. {
  17. interval = new WaitForSeconds(shootProfile.interval);
  18. rate = new WaitForSeconds(shootProfile.fireRate);
  19.  
  20. if (firePoint == null)
  21. firePoint = transform;
  22.  
  23. totalSpread = shootProfile.spread * shootProfile.amount;
  24.  
  25. StartCoroutine(ShootingSequence());
  26. }
  27.  
  28. private void OnDisable()
  29. {
  30. StopAllCoroutines();
  31. }
  32.  
  33. IEnumerator ShootingSequence()
  34. {
  35. yield return rate;
  36.  
  37. while (true)
  38. {
  39. float angle = 0f;
  40.  
  41. for (int i = 0; i < shootProfile.amount; i++)
  42. {
  43. angle = totalSpread * (i / (float)shootProfile.amount);
  44. angle -= (totalSpread / 2f) - (shootProfile.spread / shootProfile.amount);
  45.  
  46. Shoot(angle);
  47.  
  48. if (shootProfile.fireRate > 0f)
  49. yield return rate;
  50. }
  51.  
  52. yield return interval;
  53. }
  54. }
  55.  
  56. void Shoot(float angle)
  57. {
  58. GameObject temp = PoolingManager.instance.UseObject(bulletPrefabs, firePoint.position, firePoint.rotation);
  59. temp.name = shootProfile.damage.ToString();
  60. temp.transform.Rotate(Vector3.up, angle);
  61. temp.GetComponent<BulletMove>().speed = shootProfile.speed;
  62. PoolingManager.instance.ReturnObject(temp, shootProfile.destroyRate);
  63. }
  64. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement