PlayerShooting.cs 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. using UnityEngine;
  2. public class PlayerShooting : MonoBehaviour
  3. {
  4. public int DamagePerShot = 20;
  5. private int damage;
  6. public float TimeBetweenBullets = 0.15f;
  7. public float Range = 100;
  8. public GameObject Efect;
  9. float timer;
  10. Ray shootRay;
  11. RaycastHit shootHit;
  12. int shootableMask;
  13. ParticleSystem gunParticles;
  14. LineRenderer gunLine;
  15. AudioSource gunAudio;
  16. Light gunLight;
  17. float effectsDisplayTime = 0.2f;
  18. PlayerHealth PH;
  19. [SerializeField]
  20. private GameObject Boom;
  21. void Awake()
  22. {
  23. shootableMask = LayerMask.GetMask("Shootable");
  24. gunParticles = GetComponent<ParticleSystem>();
  25. gunLine = GetComponent<LineRenderer>();
  26. gunAudio = GetComponent<AudioSource>();
  27. gunLight = GetComponent<Light>();
  28. PH = transform.parent.GetComponent<PlayerHealth>();
  29. damage = DamagePerShot + PH.Level * 5;
  30. }
  31. void Update()
  32. {
  33. timer += Time.deltaTime;
  34. if (Input.GetKey(KeyCode.Mouse0) && timer >= TimeBetweenBullets && Time.timeScale != 0)
  35. {
  36. Shoot();
  37. }
  38. if ((Input.GetMouseButtonDown(1) || Input.GetKeyDown(KeyCode.E)) && BombUI.instance.CurrBombNum>0)
  39. {
  40. CreateBoom();
  41. }
  42. if (timer >= TimeBetweenBullets * effectsDisplayTime)
  43. {
  44. DisableEffects();
  45. }
  46. damage = DamagePerShot + PH.Level * 5;
  47. }
  48. void Shoot()
  49. {
  50. timer = 0f;
  51. gunAudio.Play();
  52. gunLight.enabled = true;
  53. gunParticles.Stop();
  54. gunParticles.Play();
  55. gunLine.enabled = true;
  56. gunLine.SetPosition(0, transform.position);
  57. shootRay.origin = transform.position;
  58. shootRay.direction = transform.forward;
  59. if(Physics.Raycast(shootRay,out shootHit, Range, shootableMask))
  60. {
  61. EnemyHealth enemyHealth = shootHit.collider.GetComponent<EnemyHealth>();
  62. if (enemyHealth != null)
  63. {
  64. enemyHealth.TakeDamage(damage, shootHit.point);
  65. Instantiate(Efect, shootHit.point, Quaternion.identity);
  66. }
  67. gunLine.SetPosition(1, shootHit.point);
  68. }
  69. else if(Physics.Raycast(shootRay,out shootHit, Range, LayerMask.GetMask("Floor")))
  70. {
  71. gunLine.SetPosition(1, shootHit.point);
  72. Instantiate(Efect, shootHit.point, Quaternion.identity);
  73. }
  74. else
  75. {
  76. gunLine.SetPosition(1, shootRay.origin + shootRay.direction * Range);
  77. }
  78. }
  79. void CreateBoom()
  80. {
  81. BombUI.instance.CurrBombNum -= 1;
  82. Vector3 pos = PlayerMovement.instance.GetMousePoint;
  83. pos.y = 2f;
  84. GameObject bomb = Instantiate(Boom, transform.position, Quaternion.identity) as GameObject;
  85. bomb.GetComponent<Bomb>().damage = DamagePerShot*2 + PH.Level * 7;
  86. bomb.GetComponent<Rigidbody>().AddForce(pos*50 );
  87. }
  88. public void DisableEffects()
  89. {
  90. gunLine.enabled = false;
  91. gunLight.enabled = false;
  92. }
  93. }