Advertisement
rdgorodrigo

Untitled

Aug 12th, 2015
351
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.26 KB | None | 0 0
  1. using UnityEngine;
  2. using UnityEngine.UI;
  3. using System.Collections;
  4.  
  5. public class PlayerHealth : MonoBehaviour
  6. {
  7. public int startingHealth = 100; // The amount of health the player starts the game with.
  8. public int currentHealth; // The current health the player has.
  9. public Slider healthSlider; // Reference to the UI's health bar.
  10. public Image damageImage; // Reference to an image to flash on the screen on being hurt.
  11. public AudioClip deathClip; // The audio clip to play when the player dies.
  12. public float flashSpeed = 5f; // The speed the damageImage will fade at.
  13. public Color flashColour = new Color(1f, 0f, 0f, 0.1f); // The colour the damageImage is set to, to flash.
  14. public Transform target;
  15.  
  16.  
  17.  
  18. public AudioSource sounds;
  19. public AudioClip vida;
  20. public AudioClip dano;
  21. public AudioClip dano2;
  22. public AudioClip dano3;
  23. bool isDead; // Whether the player is dead.
  24. bool damaged; // True when the player gets damaged.
  25. public int PlayAudio;
  26.  
  27. void Awake ()
  28. {
  29. sounds = GetComponent<AudioSource>();
  30.  
  31. // Set the initial health of the player.
  32. currentHealth = startingHealth;
  33. }
  34.  
  35.  
  36. void Update ()
  37. {
  38. PlayAudio = Random.Range(1, 4);
  39. if(currentHealth <= 0 && !isDead)
  40. {
  41. // ... it should die.
  42. isDead = false;
  43. sounds.PlayOneShot(deathClip,1.5F);
  44. StartCoroutine("Respawn", 5f);
  45. healthSlider.normalizedValue = 100;
  46. currentHealth = 100;
  47. }
  48.  
  49.  
  50. // If the player has just been damaged...
  51. if(damaged)
  52. {
  53. // ... set the colour of the damageImage to the flash colour.
  54. damageImage.color = flashColour;
  55. }
  56. // Otherwise...
  57. else
  58. {
  59. // ... transition the colour back to clear.
  60. damageImage.color = Color.Lerp (damageImage.color, Color.clear, flashSpeed * Time.deltaTime);
  61. }
  62.  
  63. // Reset the damaged flag.
  64. damaged = false;
  65. }
  66.  
  67.  
  68. public void TakeDamage (int amount)
  69. {
  70. // Set the damaged flag so the screen will flash.
  71. damaged = true;
  72.  
  73. // Reduce the current health by the damage amount.
  74. currentHealth -= amount;
  75.  
  76. // Set the health bar's value to the current health.
  77. healthSlider.value = currentHealth;
  78.  
  79. // Play the hurt sound effect.
  80. Playsound ();
  81.  
  82. // If the player has lost all it's health and the death flag hasn't been set yet...
  83.  
  84. }
  85.  
  86.  
  87. public void TakeHealth (int amount)
  88. {
  89.  
  90. // Reduce the current health by the damage amount.
  91. currentHealth += amount;
  92.  
  93. // Set the health bar's value to the current health.
  94. healthSlider.value = currentHealth;
  95.  
  96.  
  97. // Play the hurt sound effect.
  98. sounds.PlayOneShot(vida,0.8F);
  99.  
  100. }
  101.  
  102. void Playsound ()
  103. {
  104. if (PlayAudio == 1)
  105. {
  106. sounds.PlayOneShot(dano,1.0F);
  107. }
  108. if (PlayAudio == 2)
  109. {
  110. sounds.PlayOneShot(dano2,1.0F);
  111. }
  112. if (PlayAudio == 3)
  113. {
  114. sounds.PlayOneShot(dano3,1.0F);
  115. }
  116. }
  117. IEnumerator Respawn(float spawnDelay)
  118. {
  119. yield return new WaitForSeconds ( 1.7f );
  120.  
  121. // Change player location to active checkpoint
  122. this.transform.position = target.position;
  123. }
  124. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement