emlattimore

PlayerHealth

Apr 30th, 2019
42
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.41 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class PlayerHealth : MonoBehaviour
  6. {
  7.     public int startingHealth = 20;                        // The health the player starts with at the beginning of the game.
  8.     public int currentHealth;                              // The current amount of health the player has.
  9.  
  10.     public GameObject bluePlayer;                          // References the blue player's game object.
  11.     PlayerMovement playerMovement;                         // References the PlayerMovement script.
  12.  
  13.     bool isDead;                                           // Determines if the player is dead or not.
  14.     bool damaged;                                          // Determines if the player was just damaged.
  15.  
  16.     private void Awake()
  17.     {
  18.         playerMovement = GetComponent<PlayerMovement>();   // References the PlayerMovement script.
  19.  
  20.         currentHealth = startingHealth;                    // Sets the player's current health to the starting health.
  21.     }
  22.  
  23.     private void Update()
  24.     {
  25.         if (damaged)
  26.         {
  27.             TakeDamage(5);
  28.         }
  29.         damaged = false;                                   // If the player is damaged, deal 5 damage,
  30.                                                            // then set the damaged state to false.
  31.     }
  32.  
  33.     public void TakeDamage (int amount)
  34.     {
  35.         damaged = true;                                    // Sets the damaged state to true.
  36.  
  37.         currentHealth -= amount;                           // Subtracts the amount of health that the enemy
  38.                                                            // deals, specified in the different enemy Attack scripts.
  39.  
  40.         if (currentHealth <= 0 && !isDead)
  41.         {
  42.             Death();
  43.         }                                                  // If the player's current health is less than or
  44.                                                            // equal to 0 and the player isn't dead already,
  45.                                                            // kill the player.
  46.     }
  47.  
  48.     void Death ()
  49.     {
  50.         isDead = true;                                     // Sets the isDead state to true.
  51.  
  52.         playerMovement.enabled = false;                    // Disables the player's movement.
  53.         Destroy(bluePlayer);                               // Removes the blue player game object from the scene.
  54.     }
  55. }
Advertisement
Add Comment
Please, Sign In to add comment