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 ();
}
}
but I don't know how to get the script to behave like in the game. How do I do that?
health-bar-undertale.jpg
(62.5 kB)
Comment
Answer by goutham12 · Oct 09, 2019 at 08:13 AM
You can use fill amount instead of scalling the image. for video refference you can check the below link
private void Start()
{
hitpoint = maxHitpoint;
UpdateHealthbar ();
}
private void UpdateHealthbar()
{
float ratio = hitpoint / maxHitpoint;
currentHealthbar.fillAmount = ratio;
}
Your answer
Follow this Question
Related Questions
How to make FunRace3D Style Player-Target Distance with UI 0 Answers
I need to activate a function in another script from a script on a different game object 0 Answers
I tried fixing "Using GUiTexture has been removed, use UI.Image instead" But it wont work 1 Answer
Canvas problem 0 Answers
Player not activating method 0 Answers