- Home /
Health Degeneration script degening to fast
In this script I am trying to make it so that the players health degenerates at a constant rate of 1 health every 1 second but it is zipping from 100 health to 0 health in about 3 seconds. Any thoughts on what I need to change to make it work properly?
static var health = 100;
var maxHealth = 100;
var dieDuration =3;
var degen = 1;
//Degenerate health and display health on the guitext
function Update(){
health -= degen * Time.deltaTime;
HealthManager.hitPoints = CharacterDamage.health;
}
//Take damage and die if health reaches 0
function ApplyDamage (amount : float){
health -= amount;
if(health < 1);
Die();
}
function Die(){
yield WaitForSeconds( dieDuration );
Destroy(gameObject);
}
//If you get destroyed restart the level
function onDestroy(){
Application.LoadLevel(0);
}
UPDATED SCRIPT
static var health : float = 100;
var maxHealth = 100;
var degen : float = 1;
function Update(){
ApplyDamage(degen*Time.deltaTime);
HealthManager.hitPoints = CharacterDamage.health;
}
function ApplyDamage (amount : float){
health -= amount;
if(health < 1)
Die();
}
function Die(){
Application.LoadLevel (0);
}
Answer by kieblera5 · Sep 09, 2011 at 06:46 PM
Set your variables to floats. They're most likely presumed to be ints, since you put whole numbers in them.
I have tried floats, and tried again after your suggestion and had the same result. it is like the health value is lowering once per frame ins$$anonymous$$d of once per second.
Just because I am not "that" great at scripting for verification I changed:
var degen = 1; to var degen : float = 1;
I also noticed that I am not dying when the counter reaches 0.
I just did this code: var health : float = 100; var degen : float = 1; function Update () { health-=degen*Time.deltaTime; Debug.Log(health); }
and it outputs correctly.
You aren't dying because you never actually call the ApplyDamage function. If you want to use it, do this:
function Update() { ApplyDamage(degen*Time.deltaTime); Health$$anonymous$$anager.hitPoints = CharacterDamage.health; }
Changing health to a float worked perfectly. Thanks. However when I substitute your line ApplyDamage(degen*Time.deltaTime); into my script and enter game mode I now go straight to a black screen for some reason.
the following works for me:
var health : float = 100; var degen : float = 1; function Update () { ApplyDamage(degen*Time.deltaTime); }
function ApplyDamage(amount : float) { health-=amount; Debug.Log(health); }
Apologies for the formatting. UA never likes it when i post code in comments.
Your answer
![](https://koobas.hobune.stream/wayback/20220612205823im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
[SOLVED] Need Help "For" loop to execute in specific Intervals 1 Answer
Update not being called every frame 2 Answers
Increase health smoothly not instantly? 2 Answers
Health Regeneration 2 Answers
How can i make my health regenerate? 1 Answer