Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- public class EnemyHealth : MonoBehaviour
- {
- [SerializeField] public int Health;
- public AudioSource hurtSound;
- public AudioSource deathSound;
- public AudioSource deathSoundAlt1;
- public AudioSource deathSoundAlt2;
- public GameObject enemy;
- public static int Damage = 0;
- private bool Dead;
- private void Update()
- {
- if(Damage > 0)
- HealthDamage(Damage);
- }
- public void HealthDamage(int DamageDealt)
- {
- Health -= DamageDealt;
- Damage = 0;
- if (Health <= 0 && !Dead)
- {
- Dead = true;
- System.Random rand = new System.Random();
- int death = rand.Next(0, 4);
- switch (death)
- {
- case 1: { deathSound.Play(); StartCoroutine(waitForDeath(deathSound)); break;}
- case 2: deathSoundAlt1.Play(); StartCoroutine(waitForDeath(deathSoundAlt1)); break;
- case 3: deathSoundAlt2.Play(); StartCoroutine(waitForDeath(deathSoundAlt2)); break;
- }
- }
- else if (!Dead)
- hurtSound.Play();
- }
- IEnumerator waitForDeath(AudioSource sound)
- {
- yield return new WaitWhile(() => sound.isPlaying);
- Destroy(enemy);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement