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.
Try with Invoke: Unity - Scripting API: MonoBehaviour.InvokeRepeating
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 (Unity - Scripting API: MonoBehaviour.CancelInvoke).
I hope it helped.
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.....
}
}