- Home /
Health Regeneration
I have a damage receiver script and in it, I wrote a script to regenerate health:
#pragma strict
var hitPoints = 100.0;
var regen = 10;
var detonationDelay = 0.0;
var explosion : GameObject;
var deadReplacement : Rigidbody;
function Update() {
if(hitPoints <= 100) {
hitPoints += regen * Time.deltaTime;
}
}
The function update of course being the regeneration part. Well it's not working. Here's the whole script:
#pragma strict
var hitPoints = 100.0;
var regen = 10;
var detonationDelay = 0.0;
var explosion : GameObject;
var deadReplacement : Rigidbody;
function Update() {
if(hitPoints <= 100) {
hitPoints += regen * Time.deltaTime;
}
}
function ApplyDamage (damage : float) {
hitPoints -= damage;
if (hitPoints <= 0.0) {
var emitter : ParticleEmitter = GetComponentInChildren(ParticleEmitter);
if (emitter)
emitter.emit = true;
Invoke("DelayedDetonate", detonationDelay);
}
}
function DelayedDetonate () { BroadcastMessage ("Detonate"); }
function Detonate () {
if (explosion)
Instantiate (explosion, transform.position, transform.rotation);
if (deadReplacement) {
var dead : Rigidbody = Instantiate(deadReplacement, transform.position, transform.rotation);
dead.rigidbody.velocity = rigidbody.velocity;
dead.angularVelocity = rigidbody.angularVelocity;
}
var emitter : ParticleEmitter = GetComponentInChildren(ParticleEmitter);
if (emitter) {
emitter.emit = false;
emitter.transform.parent = null;
}
Destroy (gameObject);
}
I'm not sure why it's not working. The regen should be adding to it every second by ten, but, nothing is happening. Why is this?
For performance, regen should be float (10.0), but that shouldn't cause this problem, since multiplying by float should convert it.
Add some debugs. Is Update being called? (won't be if you subclass and don't call super.Update)
I'm still new to scripting and I pretty much skipped debugging when I was learning so I'm not sure how to debug. Oh, and Update is being called because the script that is up is not the whole script, and there are some other things in Update which are working.
Just print values to debug, I.e. put in the if:
Debug.Log("value was "+hitPoints);
Okay, I'm gonna put this in the question but, I just realized the script only regenerates when I'm below a certain amount of health. That's when it starts regenerating, and when it gets to the same amount, it stops regenerating, it doesn't regenerate. Then I realized that it only regenerates if it's below 100. Well the player has 500 health. So I changed the script. It works now.
Oh, and thank you for the debugging. I'll look into it now. Oh, and you should post an answer so I can say it's correct.
Answer by DayyanSisson · Aug 30, 2011 at 12:04 AM
Okay, since the question has been answered by myself (look at the comments of the question) I'm just posting this to say it's correct. You are free to use the script as you like.
Answer by AngryMarine · Aug 29, 2011 at 02:25 AM
This is how I calculate regeneration. This uses 3 variables (all floats) but only 1 is changed so it really doesn't impact performance. Also, I pair this with a death check formatted very similar that uses a <= 0 check in the same IF so that I don't get regen/death check conflicts.
if ( structuralIntegrity < maxStructuralIntegrity ) {
// Death Check goes here with a return(); to prevent further regen
structuralIntegrity += integrityRegen * Time.deltaTime;
}
Hope this helps!
Thanks, but if you read the comments, I already answered the question. Thanks a lot though!
Your answer
Follow this Question
Related Questions
regenerating health 3 Answers
Increase health smoothly not instantly? 2 Answers
Health Question 1 Answer
Damage script 0 Answers
From script A in script B 1 Answer