WeaponEnemy.cs 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. public class WeaponEnemy : MonoBehaviour
  5. {
  6. public float powerSuckDelay = 0.3f;
  7. private IEnumerator drainCoroutine;
  8. ParticleSystem particles;
  9. void Awake()
  10. {
  11. particles = GetComponent<ParticleSystem>();
  12. }
  13. void OnTriggerEnter(Collider other)
  14. {
  15. if (other.tag == "Player")
  16. {
  17. drainCoroutine = DrainPower(other.GetComponent<Player>());
  18. StartCoroutine(drainCoroutine);
  19. particles.Play();
  20. particles.Emit(15);
  21. }
  22. }
  23. void OnTriggerExit(Collider other)
  24. {
  25. if (other.tag == "Player")
  26. {
  27. if (drainCoroutine != null)
  28. StopCoroutine(drainCoroutine);
  29. particles.Stop();
  30. }
  31. }
  32. private IEnumerator DrainPower(Player player)
  33. {
  34. while (true)
  35. {
  36. yield return new WaitForSeconds(powerSuckDelay);
  37. player.PowerLevel -= 1.0f;
  38. }
  39. }
  40. }