Last active
June 20, 2016 10:58
-
-
Save soraphis/b8469db01751fd37abd834bcf7dc5348 to your computer and use it in GitHub Desktop.
video of the script can be found here: https://youtu.be/u-3WI5WzO7U
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 UnityEngine; | |
public class HealthBarRenderer : MonoBehaviour { | |
/// <summary> | |
/// This attributes should be read from a unit's Health-Component | |
/// </summary> | |
[Header("Health Attributes")] | |
public int Health; | |
public int PhysicalShield; | |
public int MagicalShield; | |
public int MaxHealth; | |
[Header("Canvas Elements")] | |
public RectTransform[] Bars; | |
public RectTransform[] Amounts; | |
void Update() { | |
// this validation should be a unit's health-component | |
Health = Mathf.Clamp(Health, 0, MaxHealth); | |
PhysicalShield = Mathf.Max(PhysicalShield, 0); | |
MagicalShield = Mathf.Max(MagicalShield, 0); | |
// relevant part beginning here: | |
var max = Mathf.Max(MaxHealth, Health + PhysicalShield + MagicalShield); | |
float HealthPercent = (float)Health/max; | |
float PhysPercent = (float)PhysicalShield/max; | |
float MagPercent = (float)MagicalShield/max; | |
Bars[0].anchorMax = new Vector2(HealthPercent, 1); | |
Bars[1].anchorMin = new Vector2(HealthPercent, 0); | |
Bars[1].anchorMax = new Vector2(HealthPercent + PhysPercent, 1); | |
Bars[2].anchorMin = new Vector2(HealthPercent + PhysPercent, 0); | |
Bars[2].anchorMax = new Vector2(HealthPercent + PhysPercent + MagPercent, 1); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment