How do you create decreasing health over time?
I'm making a game where you are a medic on a battlefield and you have to heal several injured soldiers that are meant to lose a health point every couple of seconds. I am not sure exactly how to go about setting up the health to decrease over time - in the code.
I'm assu$$anonymous$$g you know how to decrease health. Just look up how to do "things" over time.
Tatanan shows a typical continuous change. But "unity do something every x seconds" has lots of results.
Answer by Tatanan · Jan 09, 2015 at 04:37 PM
The easiest way is using Update and FixedUpdate methods of a MonoBehaviour and then decreasing your health var by substracting a value proporcional to Time.deltaTime. Your code will be something like:
using UnityEngine;
using System.Collections;
public class HealthUpdate : MonoBehaviour
{
public float health = 100.0f;
private const float coef = 0.2f;
void Update ()
{
health -= coef * Time.deltaTime;
}
}
Hello, I am very to Unity. So please excuse my mistakes.
I did exactly as you commented. Healthbar is decreasing in the inspector panel, But nothing happens to the actual Healthbar in the game.
I have attached the script to Healthbar but the changes aren't getting affected. Please help. @Tatanan
@Dheeraj_Singhani : This answer is 3 years old. Not sure you will get an answer from Tatanan.
It's normal your health bar does not change because the script given by Tatanan does not handle it (since OP did not mention he needed it).
Depending on how you manage your health bar (image? slider?), you will have to make some adjustment to the code. An easy and flexible way is to do as follow:
using UnityEngine;
using System.Collections;
using UnityEngine.Events;
[System.Serializable]
public class HealthEvent : UnityEvent<float> { };
public class HealthUpdate : $$anonymous$$onoBehaviour
{
public float health = 100.0f;
public HealthEvent healthEvent ;
private const float coef = 0.2f;
void Update ()
{
health -= coef * Time.deltaTime;
if( healthEvent != null )
healthEvent.Invoke( health ) ;
}
}
Then, in your inspector, you will see a "block" similar to the OnClick
block of a UI button. Just add the gameObject + function you want to update your UI.
So how much of life will decrease in that code? is it per second or $$anonymous$$ute?
Answer by unity_xgvElRcu9eitDw · Mar 29, 2019 at 02:15 PM
void Update() { if (hp > 0) { count += Time.deltaTime;
while (count > 5)
{
hp = hp - 1;
HP.text = hp.ToString();
count = 0;
}
}
}
Answer by unity_xgvElRcu9eitDw · Mar 29, 2019 at 01:06 AM
void Update() { if (hp > 0) { count += Time.deltaTime;
while (count > 5)
{
hp = hp - 1;
HP.text = hp.ToString();
count = 0;
}
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220613180006im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Health Bar issue 0 Answers
Tool Durability if Hit any Collider 1 Answer
Help me with health script 2 Answers
How to make health and damage script function correctly? 3 Answers