- Home /
Checking for a value overtime
Hey guys, I am working on a simple health system but it turns out to be a little more complex as I started writing it. I am looking for a way to decrease my player's health over time UNLESS it makes score. Now I have the score script done which simply increases the value of score by 1.
Now to make it work I will have to check if in the past 5 seconds player has done any score, if yes then ignore if not then decrease health by 10 then again repeat until it reaches 0 and game ends.
Any idea on how to implement this? Can use
int delayTime = 5;
if(delatTime > 0)
{
delayTime -= 1*Time.time;
}
I honestly don't know weather to put it in update or make a new function and make the function called in update or how else to get this done. Basically I can't figure out how to compare the two scores, one before 5 seconds and one currently. If they're same - BAM. If not everything is happy.
Answer by smallbit · Jul 16, 2014 at 08:55 AM
Here is a simple solution, When player scores call Scored() method which starts the timer. The flag scoredLastFiveSeconds will remain true for the next 5 seconds, and than will be switched to false again. You can use this bool than to check whether to apply dmg or not in the method you have.
Regards!
bool scoredInLastFiveSeconds; //flag that check if player scored
float scoreTimer; //timer
void Scored()//when player scores call this method to init counter
{
scoredInLastFiveSeconds = true; //flag to true
scoreTimer = 0f; //reset timer
}
void Update()
{
//timer
if (scoredInLastFiveSeconds) //count time only if player scored
{
if (scoreTimer < 5f) //check if time below 5 secs
{
scoreTimer += Time.deltaTime; //update timer
}
else
{
scoredInLastFiveSeconds = false; //after 10 secs set flag to false
}
}
//use flag to apply dmg
if (!scoredInLastFiveSeconds) //here you check whether to apply dmg or not
{
ApplyDamage(); //apply damaga.....
}
}
Wow there are still people here going full "I'll write the script for you" haha. Thanks, this -although not out of box- after a bit tweaking here and there worked.
Answer by MrAkroMenToS · Jul 16, 2014 at 08:37 AM
Try with Invoke: http://docs.unity3d.com/ScriptReference/MonoBehaviour.InvokeRepeating.html
InvokeRepeating("Decrease", 5, 5);
void Decrease() {
if(youDidntDoIt)
player.Health -= 10;
}
This method: call invokerepeating - 5 sec - check is you did what you have to do - if not player health -=10 - if yes nothing happens - this repeats until you stop the invoke (http://docs.unity3d.com/ScriptReference/MonoBehaviour.CancelInvoke.html). I hope it helped.
Thanks for this man, much appreciated. It had been long forgotten in my head. Thumbs up.
Your answer
![](https://koobas.hobune.stream/wayback/20220613154218im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
How to make camera position relative to a specific target. 1 Answer
Increasing an int everytime another int reaches an amount. 1 Answer
Calling Enemy Health Adding to Player Score 0 Answers
Zombie won't die 2 Answers
Multiply float by int? 2 Answers