Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
DMD2542-Project4-AndrewSides/Final Platformer/Assets/Scripts/PlayerHealth.cs
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
76 lines (59 sloc)
2.15 KB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Collections; | |
using System.Collections.Generic; | |
using UnityEngine; | |
using UnityEngine.UI; | |
using UnityEngine.SceneManagement; | |
public class PlayerHealth : MonoBehaviour | |
{ | |
public int health = 30; | |
public GameObject deathEffect; | |
public int damage = 1; | |
public Text healthtext; | |
public GameObject hitEffect; | |
public string gameOver = "GameOver"; | |
void Start() | |
{ | |
health = 30; //sets player health | |
Sethealthtext(); | |
} | |
void Sethealthtext() | |
{ | |
healthtext.text = "HEALTH: " + health.ToString(); //allows the text to display how much health you have | |
} | |
public void TakeDamage(int damage)//Damages player on collision with enemy bullet | |
{ | |
health -= damage; | |
healthtext.text = "HEALTH: " + health.ToString(); | |
Instantiate(hitEffect, transform.position, Quaternion.identity); //allows the player to take a certain amount of damage when hit, and spawns an effect as a result | |
if (health <= 0) //checks if health is 0 | |
{ | |
Die(); | |
StartCoroutine(OnDead()); | |
} | |
} | |
void Die() //destroys enemy when health is 0 and spawns an explosion | |
{ | |
Instantiate(deathEffect, transform.position, Quaternion.identity); //spawns explosion | |
gameObject.GetComponent<SpriteRenderer>().enabled = false; | |
//destroys player when health is at 0 | |
//starts the ondead coroutine | |
} | |
IEnumerator OnDead() | |
{ | |
yield return new WaitForSeconds(3f); //makes the scene wait for 3 seconds | |
SceneManager.LoadScene(gameOver); //goes to the game over scene | |
} | |
void OnTriggerEnter2D(Collider2D other) | |
{ | |
if (other.gameObject.CompareTag("Heart")) //checks if the tag of the object is rocket | |
{ | |
if (health < 99) | |
{ | |
other.gameObject.SetActive(false); //makes object disappear when player collides with object | |
health = health + 5; | |
Sethealthtext(); //sets the object to false on collision and gives the player a powerup (rocket) and displays it on the text | |
GetComponent<AudioSource>().Play(); | |
} | |
} | |
} | |
} |