Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using UnityEngine.UI;
- using System.Collections;
- public class PlayerHealth : MonoBehaviour
- {
- public int startingHealth = 100; // The amount of health the player starts the game with.
- public int currentHealth; // The current health the player has.
- public Slider healthSlider; // Reference to the UI's health bar.
- public Image damageImage; // Reference to an image to flash on the screen on being hurt.
- public AudioClip deathClip; // The audio clip to play when the player dies.
- public float flashSpeed = 5f; // The speed the damageImage will fade at.
- public Color flashColour = new Color(1f, 0f, 0f, 0.1f); // The colour the damageImage is set to, to flash.
- public Transform target;
- public AudioSource sounds;
- public AudioClip vida;
- public AudioClip dano;
- public AudioClip dano2;
- public AudioClip dano3;
- bool isDead; // Whether the player is dead.
- bool damaged; // True when the player gets damaged.
- public int PlayAudio;
- void Awake ()
- {
- sounds = GetComponent<AudioSource>();
- // Set the initial health of the player.
- currentHealth = startingHealth;
- }
- void Update ()
- {
- PlayAudio = Random.Range(1, 4);
- if(currentHealth <= 0 && !isDead)
- {
- // ... it should die.
- isDead = false;
- sounds.PlayOneShot(deathClip,1.5F);
- StartCoroutine("Respawn", 5f);
- healthSlider.normalizedValue = 100;
- currentHealth = 100;
- }
- // If the player has just been damaged...
- if(damaged)
- {
- // ... set the colour of the damageImage to the flash colour.
- damageImage.color = flashColour;
- }
- // Otherwise...
- else
- {
- // ... transition the colour back to clear.
- damageImage.color = Color.Lerp (damageImage.color, Color.clear, flashSpeed * Time.deltaTime);
- }
- // Reset the damaged flag.
- damaged = false;
- }
- public void TakeDamage (int amount)
- {
- // Set the damaged flag so the screen will flash.
- damaged = true;
- // Reduce the current health by the damage amount.
- currentHealth -= amount;
- // Set the health bar's value to the current health.
- healthSlider.value = currentHealth;
- // Play the hurt sound effect.
- Playsound ();
- // If the player has lost all it's health and the death flag hasn't been set yet...
- }
- public void TakeHealth (int amount)
- {
- // Reduce the current health by the damage amount.
- currentHealth += amount;
- // Set the health bar's value to the current health.
- healthSlider.value = currentHealth;
- // Play the hurt sound effect.
- sounds.PlayOneShot(vida,0.8F);
- }
- void Playsound ()
- {
- if (PlayAudio == 1)
- {
- sounds.PlayOneShot(dano,1.0F);
- }
- if (PlayAudio == 2)
- {
- sounds.PlayOneShot(dano2,1.0F);
- }
- if (PlayAudio == 3)
- {
- sounds.PlayOneShot(dano3,1.0F);
- }
- }
- IEnumerator Respawn(float spawnDelay)
- {
- yield return new WaitForSeconds ( 1.7f );
- // Change player location to active checkpoint
- this.transform.position = target.position;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement