- Home /
Run coroutine only when player stays inside collider?
I'm working on a survival game right now and I have a line of code in my EnemyController.cs script that changes the variable "isAttacking" from true to false after a delay (through "yield return WaitForSeconds"). For some reason, as soo n as I run into the object, my health drops all the way to 0. Any ideas?
EnemyController.cs
void OnTriggerStay (Collider col)
{
if (col.gameObject.tag == tag && !isAttacking) {
StartCoroutine ("Attack");
isAttacking = true;
}
}
void OnTriggerExit (Collider col)
{
StopCoroutine ("Attack");
}
IEnumerator Attack ()
{
yield return new WaitForSeconds (attackDelay);
isAttacking = false;
}
Player.cs
void OnTriggerEnter (Collider col)
{
if (col.gameObject.tag == damageTag && !dead) {
canTakeDamage = true;
EnemyController enemCont = col.gameObject.GetComponent<EnemyController> ();
health -= enemCont.damage;
Debug.Log (canTakeDamage + ", " + health + ", [PLAYER HIT ONCE; COROUTINE SHOULD ONLY RUN ONCE]");
}
}
void OnTriggerStay (Collider col)
{
if (col.gameObject.tag == damageTag && !dead) {
canTakeDamage = true;
EnemyController enemCont = col.gameObject.GetComponent<EnemyController> ();
if (enemCont.isAttacking) {
health -= enemCont.damage;
}
Debug.Log (canTakeDamage + ", " + health + ", [PLAYER STAYED; RUNNING COROUTINE]");
}
}
void OnTriggerExit (Collider col)
{
canTakeDamage = false;
if (!dead) {
Debug.Log (canTakeDamage + ", " + health + ", [PLAYER LEFT; COROUTINE STOPPED]");
}
}
Comment
Your answer
Follow this Question
Related Questions
Enemy wont damage player 2 Answers
Enemy wont damage player 0 Answers
How to make player stop a certain distance away from enemy? 1 Answer
Looping Between Waypoints 1 Answer
Multiple Cars not working 1 Answer