Quantcast
Channel: Questions in topic: "enemy"
Viewing all articles
Browse latest Browse all 1488

Run coroutine only when player stays inside collider?

$
0
0
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 (); 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 (); 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]"); } }

Viewing all articles
Browse latest Browse all 1488

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>