I am trying to create a timer that begins when player OnTriggerEnter an object with the tag fire. The timer counts to 5, then resets. It is going to be used to reduce player health for a short time after player has come into contact with fire game object.
It works fine apart from when the counter resets it then won’t start again if player OnTriggerEnter a second time.
I’m sure it’s something very simple that I’m not yet aware of in Unity but I’m new coding. Any help/advice would be appreciated.
Thanks very much.
Here is the code:
var playerHealth : int = 100;
var onFire : boolean = false;
var flameTimer : int = 0;
function Start () {
}
//Tells script player has collided with fire//////////
function OnTriggerEnter(other : Collider){
if(other.gameObject.CompareTag("fire")){
onFire = true;
}
}
//Converts player health to string and puts it in GUI element////////////////
function OnGUI (){
GUI.Label (Rect (10, 10, 100 ,20), "Health: " + playerHealth.ToString());
GUI.Label (Rect (10, 20, 100, 20), "flameTimer: " + flameTimer.ToString());
GUI.Label (Rect (10, 30, 100 ,20), "onFire: " + onFire.ToString());
}
function Update () {
if (onFire == true){
flameTimer = Time.time;//increments flameTimer up
playerHealth = 100 - flameTimer;//increments playerHealth down
}
if (flameTimer >= 5){//if flameTimer is greater than or equal to 5 then stop player being on fire
onFire = false;
}
}//UPDATE END