float number displayed as an int? or at least on the health bar
So I got a health bar, hunger bar, and thirst bar. everything is good; the thirst and hunger goes down throughout time. Also got it hooked up to the canvas, so I see my visual hunger and thirst bars gradually go down, along with the %.
problem is, it displays all these crazy ass decimal numbers after the decimal. I want to cut it down to 2 digits after the decimal, or anything like that.
and in order for the visual bars to be hooked up to the invisible variable numbers, it has to be a float?
heres the script:
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI;
public class DamageAI : MonoBehaviour { public float playerHealth = 100; private float maxplayerHealth = 100; private float damage = 8;
//New shit xD
private EnemyAI DAI;
private EnemyAI EAI;
public AudioSource aSource;
private int healthReg = 1;
bool isRegenHealth;
public float hunger = 100f;
private float maxHunger = 100;
public float thirst = 100f;
private float maxThirst = 100;
private float hungerSpeedMultiplier = 0.055f;
private float thirstSpeedMultiplier = 0.155f;
void Start()
{
UpdateHealthBar();
aSource = GetComponent<AudioSource>();
EAI = gameObject.GetComponent<EnemyAI>();
DAI = gameObject.GetComponent<EnemyAI>();
}
private void Update()
{
{
UpdateHealthBar();
CheckDeath();
UpdateHNT();
}
if (hunger > 0)
{
hunger -= Time.deltaTime * hungerSpeedMultiplier;
}
if (thirst > 0)
{
thirst -= Time.deltaTime * thirstSpeedMultiplier;
}
}
private void CheckDeath()
{
if (playerHealth <= 0)
{
Die();
}
}
private void Die()
{
Destroy(GetComponent<Movement>());
Destroy(GetComponent<EnemyAI>());
Destroy(GetComponent<DamageAI>());
}
private void UpdateHealthBar()
{
if(playerHealth != maxplayerHealth && !isRegenHealth)
{
StartCoroutine(RegainHealthOverTime());
}
if (playerHealth > maxplayerHealth)
{
playerHealth = maxplayerHealth;
}
}
private IEnumerator RegainHealthOverTime()
{
isRegenHealth = true;
while (playerHealth < maxplayerHealth)
{
Healthregen();
yield return new WaitForSeconds(3);
}
isRegenHealth = false;
}
public void Healthregen()
{
playerHealth += healthReg;
}
private void UpdateHNT()
{
if(hunger > maxHunger)
{
hunger = maxHunger;
}
if(thirst > maxThirst)
{
thirst = maxThirst;
}
if (hunger <= 0 && (thirst <= 0))
{
playerHealth -= Time.deltaTime / damage * 16;
}
else if (hunger <= 0 || thirst <= 0)
{
playerHealth -= Time.deltaTime / damage * 16;
}
if(hunger < 50f)
{
EAI.lookAtF();
EAI.eatPlease();
}
if(thirst < 50f)
{
DAI.lookAtD();
DAI.drinkPlease();
}
}
void OnCollisionEnter(Collision _collision)
{
if (_collision.gameObject.tag == "Enemy")
{
playerHealth -= damage;
aSource.Play();
{
if (playerHealth < 0)
{
playerHealth = 0;
}
UpdateHealthBar();
}
}
}
public void HealDamage(float heal)
{
playerHealth += heal;
if (playerHealth > maxplayerHealth)
playerHealth = maxplayerHealth;
UpdateHealthBar();
}
}
Ok maybe that was too much. That was the whole script. Here are the important parts:
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI;
public class DamageAI : $$anonymous$$onoBehaviour { public float playerHealth = 100; private float maxplayerHealth = 100; private float damage = 8;
public float hunger = 100f;
public float thirst = 100f;
private float hungerSpeed$$anonymous$$ultiplier = 0.055f;
private float thirstSpeed$$anonymous$$ultiplier = 0.155f;
void Start()
{
}
private void Update()
{
{
UpdateHNT();
}
if (hunger > 0)
{
hunger -= Time.deltaTime * hungerSpeed$$anonymous$$ultiplier;
}
if (thirst > 0)
{
thirst -= Time.deltaTime * thirstSpeed$$anonymous$$ultiplier;
}
}
private void UpdateHNT()
{
if (hunger <= 0 && (thirst <= 0))
{
playerHealth -= Time.deltaTime / damage * 16;
}
else if (hunger <= 0 || thirst <= 0)
{
playerHealth -= Time.deltaTime / damage * 16;
}
}
}
}