- Home /
player on fire timer
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
If you remove if (flameTimer >= 5){
onFire = false;
}
It will not disable itself. That is the reason it won't repeat. You will also need to make: if (onFire == true){
flameTimer = Time.time;//increments flameTimer up
playerHealth = 100 - flameTimer;
if (flameTimer < 1)
{
flameTimer = 5; //Reset the flame timer.
}
}
Hope this guides you!
Answer by iwaldrop · Nov 19, 2013 at 09:54 PM
Maybe you should try using the InvokeRepeating method.
InvokeRepeating("Fire", 0, flameTime);
Then, to cancel firing, call CancelInvoke.
CancelInvoke("Fire");
Of course, you'll need a Fire method:
function Fire()
{
// firing logic goes here
}
Answer by Raccoonking · Nov 20, 2013 at 02:28 AM
Thank you slayer29179 and iwaldrop. Much appreciated. In the end I found a different approach which may be of interest.
I used a yield WaitForSeconds (5); http://docs.unity3d.com/Documentation/ScriptReference/WaitForSeconds-ctor.html
Ended up being a tidy way to write it. It's not that precise, I end up removing 6-7 points from player health but for the time being it will do.
Thanks again for the help :)
Here it is:
var playerHealth : int = 100;
//TRAPS DAMAGE///////////////////////////////////
var onFire : boolean = false;
//on fire timer
function OnTriggerEnter(other : Collider){
if(other.gameObject.CompareTag("fire")){
onFire = true;
yield WaitForSeconds (5);//wait for 5 seconds
onFire = false;
}
}
function Start () {
}//START END///////////////////////////////////
//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, 30, 100 ,20), "onFire: " + onFire.ToString());
}
function Update () {
//Flame ///////////////
if (onFire == true){
playerHealth = 100 - Time.time;
}
}//UPDATE END
What you're saying is that for five seconds after entering a collider with the tag of "fire" the playerHealth will be set to 100 - the time of that frame since startup.
Time.time is the time of the frame since startup. Your logic states that if this occurs more than 100 seconds after the beginning of the game, then the player's health will be set to a negative number.
This kind of logic is flawed, because the player can enter and exit these zones at will, right? So what happens if he enters it at 5 seconds, and then again at 9 seconds? Will it still stop hurting him at 10? Yes, because the collision event is setting onFire to false. Ideally you want to call a method from the collision that handles whatever you want to do, like so:
var fireDuration : float;
var fireDamagePerHit : float;
private var isFiring : bool;
function OnTriggerEnter(other : Collider)
{
ProcessTriggerEntered(other.tag)
}
function ProcessTriggerEntered(string tagName)
{
switch(tagName)
{
case "fire":
Fire();
break;
}
}
function Fire()
{
if (isFiring)
return;
float startTime = Time.time;
float endTime = startTime + fireDuration;
while (endTime > Time.time)
{
playerHealth -= fireDamagePerHit;
yield return null;
}
isFiring = false;
}
Sorry if some of this doesn't compile, I work in C#. :)
Also, if you want multiple things to damage the player simultaneously then you need to track what is damaging him and increment/decrement a counter when firing starts and stops.
But all of this is an aside to the fact that this is the wrong way to do it. You should attach a script that will deal damage the player, not attach a script to the player that will damage himself based on whether he has collided with something.
While everything I said is technically true, I just noticed what you're trying to do. Set the player on fire. Your Boolean flag should be called isOnFire, not onFire. Self documenting code is the best kind of code.
In that case, you're basically right in your thinking, and the script should be somewhere on the player, so belay my last. However, it would be best to have a fire effect game object that has a particle emitter and a script. That script should activate and deactivate the fire effect, while damaging he player while it's active.
var duration : float = 5;
private var endTime;
function Activate()
{
endTime = Time.time + duration;
enabled = true;
}
function Deactivate()
{
enabled = false;
}
function OnEnable()
{
// start particle effect
}
function OnDisabled()
{
// stop fire effect
}
function Update()
{
// do damage to player
enabled = endTime > Time.time;
}
This way, you have a discrete class that handles the effect and does damage, can be activated repeatedly, and will always keep the player on fire for the duration you define.
Your answer
Follow this Question
Related Questions
Stop watch for 100 meter dash 1 Answer
Can anyone help ?? Timer and gui 0 Answers
How to create a homing script 1 Answer
C# countdown timer 9 Answers
How to realize accurate time counter (millisecond precision) 3 Answers