- Home /
How do I create an undertale player health bar in C #?
I'm trying to make this health bar:
and I am using this script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class UndertaleHealthbar : MonoBehaviour
{
public Image currentHealthbar;
public Image restHealthbar;
public Text ratioText;
public float hitpoint;
public float maxHitpoint;
private void Start()
{
UpdateHealthbar ();
}
private void UpdateHealthbar()
{
float ratio = hitpoint / maxHitpoint;
currentHealthbar.rectTransform.localScale = new Vector3 (ratio, 1, 1);
restHealthbar.rectTransform.localScale = new Vector3 (ratio, 1, 1);
ratioText.text = (ratio * 20).ToString ("0");
}
private void TakeDamage(float damage)
{
hitpoint -= damage;
if (hitpoint < 0)
{
hitpoint = 0;
Debug.Log("Dead!");
}
UpdateHealthbar ();
}
private void HealDamage(float heal)
{
hitpoint += heal;
if (hitpoint > maxHitpoint)
{
hitpoint = maxHitpoint;
}
UpdateHealthbar ();
}
}
Where currentHealthbar is the yellow bar and restHealthbar would be purple, but I don't know how to get the script to behave like in the game. How do I do that?
Answer by GrayLightGames · Oct 09, 2019 at 04:12 AM
I can't tell how you want the restHealthbar mechanics to work... is it a constant amount of health more than the current hitpoints or a percentage? Either way, create and calculate a restHitpoint variable based on hitPoint, then calculate the retHitpoint ratio and then change line 23 to restHealthbar.rectTransform.localScale = new Vector3 (restHitpoint / maxHitpoint, 1, 1); and you should be in business. Hope that helps!
In this case it is for the yellow bar to be the total points and the purple bar to follow the yellow bar, as if it is bleeding or on fire until the purple bar normalizes with the yellow bar.
Ah yes, that is a cool effect. Good luck!