Question by
nroesner · Jan 18, 2018 at 02:07 PM ·
timersliderhealthbartimer countdownc# tutorial
How do I decrease a slider over time?
Hello guys and gals,
the general idea is to create a slider which operates like health bar which decreases constantly over time so that the game is timebound and over after 5ish minutes. So far I have tried to set a countdown for the slider and entered the value of the total time to run down to zero like this:
public class Countdown : MonoBehaviour {
public float timeLeft = 32100.0f;
public bool stop = true;
private float minutes;
private float seconds;
public void startTimer(float from)
{
stop = false;
timeLeft = from;
Update();
}
void Update()
{
if (stop) return;
timeLeft -= Time.deltaTime;
minutes = Mathf.Floor(timeLeft / 60);
seconds = timeLeft % 60;
if (seconds > 59) seconds = 59;
if (minutes < 0)
{
stop = true;
minutes = 0;
seconds = 0;
}
// fraction = (timeLeft * 100) % 100;
}
}
A very big thank you in advance
Comment
Answer by PersianKiller · Jan 18, 2018 at 03:11 PM
hey its just an easy example I hope it gives you the idea.
public Slider HealthBar;
public float health;
public float MaxHealth;// 1000
void Start(){
health=MaxHealth;
HealthBar= GetComponent<Slider> ();
HealthBar.maxValue =MaxHealth;
HealthBar.value = health;
}
void Update(){
health-=0.1f;
HealthBar.value = health;
}
Awesome thank you very much it works perfectly fine now. Just the dot after the 1 in line 16 was to much