- Home /
Why is this Int lagging when its subtracting something over a period of time?
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
public class hthScript : MonoBehaviour {
//begining
public int maxHealth = 100;
public int maxThirst = 100;
public int maxHunger = 100;
//realtime
public int currentHealth;
public int currentThirst;
public int currentHunger;
public float hungerLoss;
public float thirstLoss;
//references
public Slider healthSlider;
public Slider thirstSlider;
public Slider hungerSlider;
// Use this for initialization
void Start () {
currentHealth = maxHealth;
currentThirst = maxThirst;
currentHunger = maxHealth;
}
// Update is called once per frame
void Update () {
HTHDecline();
healthSlider.value = currentHealth;
thirstSlider.value = currentThirst;
hungerSlider.value = currentHunger;
}
//when something happens:
public void TakeDamage(int amount)
{
currentHealth -= amount;
}
public void DrinkWater(int amount)
{
currentThirst += amount;
}
public void EatFood(int amount)
{
currentHunger += amount;
}
//Over a period of time:
public void HTHDecline()
{
//For hunger
currentHunger = currentHunger - Mathf.RoundToInt(Time.deltaTime * hungerLoss);
//For thirst
currentThirst = currentThirst - Mathf.RoundToInt(Time.deltaTime * thirstLoss);
//thirst & hunger kills you:
if (currentHunger < 25)
{
currentHealth = currentHealth - Mathf.RoundToInt(Time.deltaTime * 20);
}
if (currentThirst < 25)
{
currentHealth = currentHealth - Mathf.RoundToInt(Time.deltaTime * 20);
}
}
}
It will count down from 100 and stop at random numbers and continue on the currentHunger and the currentThirst variables.
I don't completely understand what you're asking, but currentHealth = currentHealth - $$anonymous$$athf.RoundToInt(Time.deltaTime * 20);
seems like a problem. Unless your game is running slow, $$anonymous$$athf.RoundToInt(Time.deltaTime * 20);
will always equal 0.
Basically im trying to make the hunger slider slowly count down and when the hunger bar is at 25 it will start to hurt the player. For some reason the hunger bar will start to count down and stop and go again.
Answer by joeross0 · Dec 31, 2017 at 03:56 AM
using UnityEngine; using UnityEngine.UI; using System.Collections;
public class hthScript : MonoBehaviour {
//begining
public float maxHealth = 100;
public float maxThirst = 100;
public float maxHunger = 100;
//realtime
public float currentHealth;
public float currentThirst;
public float currentHunger;
public float hungerLoss;
public float thirstLoss;
//references
public Slider healthSlider;
public Slider thirstSlider;
public Slider hungerSlider;
// Use this for initialization
void Start()
{
currentHealth = maxHealth;
currentThirst = maxThirst;
currentHunger = maxHealth;
}
// Update is called once per frame
void Update()
{
HTHDecline();
healthSlider.value = currentHealth;
thirstSlider.value = currentThirst;
hungerSlider.value = currentHunger;
}
//when something happens:
public void TakeDamage(int amount)
{
currentHealth -= amount;
}
public void DrinkWater(int amount)
{
currentThirst += amount;
}
public void EatFood(int amount)
{
currentHunger += amount;
}
//Over a period of time:
public void HTHDecline()
{
//For hunger
currentHunger = currentHunger - Time.deltaTime * hungerLoss;
//For thirst
currentThirst = currentThirst - Time.deltaTime * thirstLoss;
//thirst & hunger kills you:
if (currentHunger < 25)
{
currentHealth = currentHealth - Time.deltaTime * 20;
}
if (currentThirst < 25)
{
currentHealth = currentHealth - Time.deltaTime * 20;
}
}
}
Sooooo i found out what was wrong. It basically wasn't running smooth so I had to change all of the int's to float's