How do I make the player character take damage while standing still?
Hello everyone, So I'm trying to implement it in this game where if the player does not move their character for X amount of time they will start taking damage to their health, where it will stop taking damage when they move again. The main thing here is I don't know how to do a check to detect their current position and compare it to their previous position to see if they've moved (basically I don't know how to check if player is moving or not). If anyone has a video or knows the code on how to check that, I can do the rest. Thank you in advance for any assistance.
Answer by KenanTheFab · Feb 10, 2019 at 05:29 PM
CharacterController charControl;
bool moving;
float moveTimer = 2;
float dmgFreq = 1; /// how many seconds go between taking dmg?
ScriptThatControlsHealth Health;
void update
{
///0.3 instead of 0 will make it so that if the value doesn't hit 0 and maybe has 0.0000001 instead it will still work, you can set the "tolerance" higher or lower.
if(charControl.velocity.magnitude < 0.3)
{
StartCourotine(DealDmg()); ///Starts a loop that deals dmg every X seconds.
}
else
{
StopCourotine(DealDmg()); /// Stops the loop if the player is moving.
}
}
IEnumerator DealDMG()
{
while(true)
{
Health.HPFloat -= 1; ///The player takes 1 dmg every X seconds, X being dmgFreq, and the 1 for how much dmg the player takes every X seconds, so change this to increase or decrease dmg.
yield return new WaitForSeconds(dmgFreq);
}
}
Any questions?
Your answer
Follow this Question
Related Questions
FormatException: Input string was not in the correct format 0 Answers
Dodgeroll script crashing game 0 Answers
Errors With Game Over Script 1 Answer
Need help making a stamina bar. 0 Answers
My coroutine script isn't working, it has no errors 0 Answers