EnemyHealth.cs 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. using UnityEngine;
  2. using UnityEngine.AI;
  3. using UnityEngine.UI;
  4. public class EnemyHealth : MonoBehaviour
  5. {
  6. public int startingHealth = 100;
  7. public int currentHealth;
  8. public float sinkSpeed = 2.5f;
  9. public int scoreValue = 10;
  10. public float Exp;
  11. public AudioClip deathClip;
  12. Animator anim;
  13. AudioSource enemyAudio;
  14. bool isDead;
  15. bool playerDead;
  16. ParticleSystem hitparticles;
  17. CapsuleCollider capsulecollider;
  18. bool isSinking;
  19. public GameObject HpDisplay;
  20. public Image HpBar;
  21. public GameObject kusuri;
  22. PlayerHealth player;
  23. void Awake()
  24. {
  25. anim = GetComponent<Animator>();
  26. enemyAudio = GetComponent<AudioSource>();
  27. hitparticles = GetComponentInChildren<ParticleSystem>();
  28. capsulecollider = GetComponent<CapsuleCollider>();
  29. currentHealth = startingHealth;
  30. player = GameObject.FindGameObjectWithTag("Player").GetComponent<PlayerHealth>();
  31. }
  32. void Update()
  33. {
  34. if (isSinking)
  35. {
  36. transform.Translate(-Vector3.up * sinkSpeed * Time.deltaTime);
  37. }
  38. Transform camera = Camera.main.transform;
  39. HpDisplay.transform.LookAt(camera);
  40. HpBar.fillAmount = (float)currentHealth / (float)startingHealth;
  41. }
  42. public void TakeDamage(int amount,Vector3 hitPoint)
  43. {
  44. if (isDead)
  45. return;
  46. enemyAudio.Play();
  47. currentHealth -= amount;
  48. hitparticles.transform.position = hitPoint;
  49. hitparticles.Play();
  50. if (currentHealth <= 0 && !isDead)
  51. {
  52. Death();
  53. }
  54. }
  55. void Death()
  56. {
  57. isDead = true;
  58. System.Array.ForEach(GetComponents<Collider>(),col=>col.enabled = false);
  59. anim.SetTrigger("Dead");
  60. enemyAudio.clip = deathClip;
  61. enemyAudio.Play();
  62. if(Random.Range(0,100) < 8)
  63. {
  64. Instantiate(kusuri, transform.position+new Vector3(0,0.5f,0), Quaternion.identity);
  65. }
  66. }
  67. public void StartSinking()
  68. {
  69. GetComponent<NavMeshAgent>().enabled = false;
  70. GetComponent<Rigidbody>().isKinematic = true;
  71. isSinking = true;
  72. ScoreManager.score += scoreValue;
  73. player.ExpUpdate(Exp);
  74. Destroy(gameObject, 2f);
  75. }
  76. }