Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- public class PlayerHealth : MonoBehaviour
- {
- public int startingHealth = 20; // The health the player starts with at the beginning of the game.
- public int currentHealth; // The current amount of health the player has.
- public GameObject bluePlayer; // References the blue player's game object.
- PlayerMovement playerMovement; // References the PlayerMovement script.
- bool isDead; // Determines if the player is dead or not.
- bool damaged; // Determines if the player was just damaged.
- private void Awake()
- {
- playerMovement = GetComponent<PlayerMovement>(); // References the PlayerMovement script.
- currentHealth = startingHealth; // Sets the player's current health to the starting health.
- }
- private void Update()
- {
- if (damaged)
- {
- TakeDamage(5);
- }
- damaged = false; // If the player is damaged, deal 5 damage,
- // then set the damaged state to false.
- }
- public void TakeDamage (int amount)
- {
- damaged = true; // Sets the damaged state to true.
- currentHealth -= amount; // Subtracts the amount of health that the enemy
- // deals, specified in the different enemy Attack scripts.
- if (currentHealth <= 0 && !isDead)
- {
- Death();
- } // If the player's current health is less than or
- // equal to 0 and the player isn't dead already,
- // kill the player.
- }
- void Death ()
- {
- isDead = true; // Sets the isDead state to true.
- playerMovement.enabled = false; // Disables the player's movement.
- Destroy(bluePlayer); // Removes the blue player game object from the scene.
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment