- Home /
How to make player lose health every 2 seconds he is in a trigger?
Thanks in advance, this simple thing confuses me to much.
OnTriggerEnter () { player.Health-= 10; yield WaitForSeconds(2) player.Health-=10; }
And other methods, I just dont know how to make sure its only when he is in the water
Answer by DiegoSLTS · May 21, 2015 at 12:43 PM
I'd start a coroutine on OnTriggerEnter and stop it on OnTriggerExit. The coroutine would loop removing health and waiting for 2 seconds. Something like this:
Coroutine underwaterDamage;
void OnTriggerEnter() {
underwaterDamage = StartCoroutine(UnderwaterDamage());
}
void OnTriggerExit() {
StopCoroutine(underwaterDamage);
}
IEnumerator UnderwaterDamage() {
while (true) {
player.Health -= 10;
yield return new WaitForSeconds(2f);
}
}
If this were to work on a multiplayer game, where it finds whatever is in the waters health script, then subtracting 10 from the health variable in there, how would I make it check? Thanks a lot btw!
Your answer
Follow this Question
Related Questions
When health is 0, character still lives 2 Answers
I loose health after touched enemy and my script only displays the first bloodimage 1 Answer
Enemy Health Bar 1 Answer
Using contact point 1 Answer
Photon Network Health Script 0 Answers