Created
November 14, 2021 15:12
-
-
Save didacus/8bad739bd0d3a77e2100dcb66b66e552 to your computer and use it in GitHub Desktop.
Unity - Basic enemy health manager
This file contains hidden or 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; | |
public class EnemyHealthManager : MonoBehaviour | |
{ | |
public int startingHealth; | |
private int currentHealth; | |
public float flashLength; | |
private float flashCounter; | |
private Renderer renderColor; | |
private Color storedColor; | |
void Start() | |
{ | |
currentHealth = startingHealth; | |
// Get colours from gameObject | |
renderColor = GetComponent<Renderer>(); | |
storedColor = renderColor.material.GetColor("_Color"); | |
} | |
void Update() | |
{ | |
if (currentHealth < 0) | |
{ | |
Destroy(gameObject); | |
} | |
/// Change colour back to original after certain time | |
if (flashCounter > 0) | |
{ | |
flashCounter -= Time.deltaTime; | |
if (flashCounter <= 0) | |
{ | |
renderColor.material.SetColor("_Color", storedColor); | |
} | |
} | |
/// | |
} | |
/// Function to be called from BulletController to pass damage | |
public void DamageEnemy(int damage) | |
{ | |
currentHealth -= damage; // Apply damage | |
flashCounter = flashLength; // Set time for color change | |
renderColor.material.SetColor("_Color", Color.green); // Change color | |
} | |
/// End | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment