Advertisement
Guest User

EnemyHealth

a guest
Apr 6th, 2020
224
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.36 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class EnemyHealth : MonoBehaviour
  6. {
  7.  
  8.     [SerializeField] public int Health;
  9.  
  10.     public AudioSource hurtSound;
  11.     public AudioSource deathSound;
  12.     public AudioSource deathSoundAlt1;
  13.     public AudioSource deathSoundAlt2;
  14.  
  15.     public GameObject enemy;
  16.  
  17.     public static int Damage = 0;
  18.  
  19.     private bool Dead;
  20.  
  21.     private void Update()
  22.     {
  23.         if(Damage > 0)
  24.             HealthDamage(Damage);
  25.     }
  26.  
  27.     public void HealthDamage(int DamageDealt)
  28.     {
  29.         Health -= DamageDealt;
  30.         Damage = 0;
  31.         if (Health <= 0 && !Dead)
  32.         {
  33.             Dead = true;
  34.             System.Random rand = new System.Random();
  35.             int death = rand.Next(0, 4);
  36.             switch (death)
  37.             {
  38.                 case 1: { deathSound.Play(); StartCoroutine(waitForDeath(deathSound)); break;}
  39.                 case 2: deathSoundAlt1.Play(); StartCoroutine(waitForDeath(deathSoundAlt1)); break;
  40.                 case 3: deathSoundAlt2.Play(); StartCoroutine(waitForDeath(deathSoundAlt2)); break;
  41.             }
  42.            
  43.         }
  44.         else if (!Dead)
  45.             hurtSound.Play();
  46.     }
  47.  
  48.     IEnumerator waitForDeath(AudioSource sound)
  49.     {
  50.         yield return new WaitWhile(() => sound.isPlaying);
  51.         Destroy(enemy);
  52.     }
  53.  
  54. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement