- Home /
Help with fire damage on a health bar?
I have made a script that shows a gui texture as health, when the player loses health the gui is then replaced with another gui showing less health. The problem Im having is, I want to make a fire environment where if the player is inside the area of fire, then damage is dealt. The issue is that the player loses all lives instantly, is there a way that I can have the players life slowly reduced, for example, one life is taken away every 10 seconds? Appreciate any help.
Below is the script that I have attached to a FPC.
function OnTriggerStay(collisionInfo : Collider)
{
if(collsionInfo.gameObject.tag == "Fire")
{
HealScript.Lives -= 1;
enabled = false;
yield WaitForSeconds(10);
enabled = true;
}
}
Answer by Owen-Reynolds · May 20, 2011 at 06:06 PM
[heavily edited in response to comments]
Two ways of making a delay are setting a "not again until now" time variable, or using Wait and flags. The set var method:
float nextHurtTime = 0; // when we get hurt, set to this 10 seconds later, for delay
float hurtDelay = 10.0f; // seconds between losing life in a flame zone
// in the trigger:
if(collisionInfo.gameObject.tag == "Fire" && Time.time > nextHurtTime) {
nextHurtTime = Time.deltaTime + hurtDelay;
HealScript.Lives -= 1;
}
The delay and set var method (I use C#, so this is my guess at the javascript version):
// Public for testing. Does it stay false for 10 secs?
public var fireHurts : bool = true; // At start, fire can hurt us
function OnTriggerStay(Collider cc) : void {
if(fireHurts==false) return; // we were injured recently
if( [player in fire] ) {
fireHurts = false;
life--;
// C# uses wait in a differently, so not so sure about this:
yield WaitforSeconds(10);
fireHurts=false;
}
}
The code works for the first life, but then continues to deal damage afterwards, will I have to use the code for each life the player has?
At the start of the script, check whether enabled==true. Did you mean to use the official "enabled"? If you don't to turn off everything, use bool fireOff;
After trying the fireOff and enabled == true, I still got the same result as before. Once the player is in the collider 1 life is taken, 10 seconds later another life is meant to be taken but ins$$anonymous$$d all the lives are being lost resulting in a game over. Is there a way of delaying the next damage time and so on?
Answer by Ben 39 · May 22, 2011 at 02:25 PM
Ive finally corrected the script...working just fine now. I attached this script to the player and then tagged a collider "Fire".
var FireDamage : float = 1;
function OntriggerStay (collisionInfo : Collider)
{
if (enabled)
{
if(collisionInfo.gameObject.tag == "Fire")
{
HealthScript.LIVES -= FireDamage * Time.deltaTime * 1;
enabled = false;
yield WaitForSeconds (10);
eabled = true;
}
}
}