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;
}
}
[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;
}
}
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;
}
}
}