78 lines
2.3 KiB
C#
78 lines
2.3 KiB
C#
using UnityEngine;
|
|
using UnityEngine.UI;
|
|
|
|
public class HealthBarManager : MonoBehaviour
|
|
{
|
|
|
|
private float playerHealth;
|
|
public float sliderValue = 100.0f;
|
|
public GameObject centerBar;
|
|
private Transform centerBarTransform;
|
|
public GameObject rightBar;
|
|
private Transform rightBarTransform;
|
|
|
|
public Image centerBarSprite;
|
|
public Image rightBarSprite;
|
|
// public Image leftBarSprite;
|
|
private float shownHealth;
|
|
public Image skullSprite;
|
|
private Transform centerBarPosition;
|
|
|
|
private float centerBarOriginalHeight;
|
|
private float centerBarOriginalWidth;
|
|
private Vector2 originalSizeDelta;
|
|
|
|
|
|
// Start is called once before the first execution of Update after the MonoBehaviour is created
|
|
void Awake()
|
|
{
|
|
centerBarOriginalWidth = centerBarSprite.sprite.rect.width;
|
|
centerBarOriginalHeight = centerBarSprite.sprite.rect.height;
|
|
|
|
skullSprite.enabled = false;
|
|
|
|
originalSizeDelta = centerBarSprite.rectTransform.sizeDelta;
|
|
}
|
|
|
|
// Update is called once per frame
|
|
void FixedUpdate()
|
|
{
|
|
playerHealth = GameObject.Find("GameData").GetComponent<GameData>().CurrentHealth;
|
|
if (playerHealth > 0)
|
|
{
|
|
centerBarSprite.enabled = true;
|
|
rightBarSprite.enabled = true;
|
|
// leftBarSprite.enabled = true;
|
|
skullSprite.enabled = false;
|
|
|
|
shownHealth = playerHealth;
|
|
}
|
|
else
|
|
{
|
|
shownHealth = 0;
|
|
|
|
centerBarSprite.enabled = false;
|
|
rightBarSprite.enabled = false;
|
|
// leftBarSprite.enabled = false;
|
|
skullSprite.enabled = true;
|
|
}
|
|
|
|
// add lerp function for health
|
|
float healthIncrement = (shownHealth - sliderValue) * 0.1f;
|
|
|
|
sliderValue += healthIncrement;
|
|
|
|
|
|
centerBarSprite.rectTransform.sizeDelta = new Vector2(originalSizeDelta.x * sliderValue / 100.0f, originalSizeDelta.y);
|
|
|
|
|
|
// centerBarPosition = centerBar.GetComponent<Transform>();
|
|
|
|
|
|
centerBarTransform = centerBar.GetComponent<Transform>();
|
|
centerBarTransform.position += Vector3.right * healthIncrement * 1.1f;
|
|
rightBarTransform = rightBar.GetComponent<Transform>();
|
|
rightBarTransform.position += Vector3.right * healthIncrement * 2f * 1.1f;
|
|
}
|
|
}
|