- Home /
Need help developing Damage Over Time mechanic
Hello,
I am posting anew with this question because the last question was closed (thanks unnamed moderator).
My problem is to do with Damage Over Time. I have been working on this mechanic for a couple of days now, and I can't seem to work my way around this simple problem. I simply don't understand the IEnumerator interface! No matter what concoction I think up,I end up crashing Unity. So here I am, asking for help.
My scenario: I am making a swimming simulation. This simulation needs to be able to drown you, should you stay underwater for too long. Basically, I need the drowning effect to be slow, and odd. I figured I could do this with the coroutines available in C#. In fact, I know that I can. However, I am having trouble understanding them. When I invoke the coroutine, Unity freezes up and crashes. I'm pretty sure that this is because the routine is being called too many times, creating a memory hog.
Basically, it needs to start the coroutine after being underwater for 10 seconds. Once the coroutine starts, it will deal 15 damage every 3-5 seconds. If you stay in the water for too long, you will drown (duh). The routine needs to be able to stop once the player exits the water (moves head above 5.2 on the y axis).
At this point in time, the script crashes unity as soon as I start the program. Any idea why? It isn't supposed to start until the player has been in the water for 10 seconds, as I said earlier.
My code:
using UnityEngine;
using System.Collections;
public class suffocation : MonoBehaviour {
public GameObject SwimmerScript;
public float breathInSeconds = 10.00f;
public float timeSpentUnderwater;
public bool underwater = false;
public float startTime;
public float targetTime;
public bool canTakeDrowningDamage = true; // starts true
void Update(){
if(SwimmerScript.GetComponent<Swimmer>().pos < 5.2){
targetTime += Time.deltaTime;
underwater = true;
//targetTime++;
}
/*
if(targetTime >= 10.00f && targetTime <= 20.00f)
{
StartCoroutine("HurtMe",3.1415);
}
if(targetTime > 20.00f)
{
StopCoroutine("HurtMe");
}*/
if (underwater) {
timeSpentUnderwater += Time.deltaTime;
if (timeSpentUnderwater > breathInSeconds) {
StartCoroutine("Drowning"); // called regardless of whether we can take damage this frame, but that's okay.
}
}
else {
timeSpentUnderwater = 0f;
}
}
/*
void CheckForWater()
{
if(underwater && targetTime > 10.00f)
{
StartCoroutine(HurtMe(3.1415f));
//3.1415 seconds later
}
}
*/
IEnumerator Drowning() {
if (!canTakeDrowningDamage) yield break; // exit if not ready for more damage
canTakeDrowningDamage = false; // if we touch this line, immediately make it impossible to drown again until true
gameObject.GetComponent<Health>().health -= 25; // take damage
yield return new WaitForSeconds( 3.14f ); // wait for seconds
canTakeDrowningDamage = true; // reset to allow damage
/*
while(targetTime < (20.00f) && gameObject.GetComponent<Health>().dead == false){
gameObject.GetComponent<Health>().health -= 25;
yield return null; //wait for a frame
yield return new WaitForSeconds(delay); }
if(gameObject.GetComponent<Health>().dead == true)
StopCoroutine("HurtMe");*/
}
}
Just to confirm: if you comment out the StartCoroutine line, it no longer crashes Unity?
Your answer
Follow this Question
Related Questions
Run coroutine X amount of times in Y seconds? 3 Answers
Use for-i-loop var outside the loop 2 Answers
stop game for 1 second 2 Answers
Applying Damage 5 Answers
Slowly making the player go faster. 0 Answers