void OnCollisionEnter2D(Collision2D collision) { bool damagePlayer = false; //check if collision with enemy EnemyScript enemy = collision.gameObject.GetComponent <EnemyScript> (); if (enemy != null) { //kill enemy HealthScript enemyHealth = enemy.GetComponent <HealthScript>(); if (enemyHealth != null) { enemyHealth.DoDamage(enemyHealth.hp); } damagePlayer = true; } if (damagePlayer) { HealthScript playerHealth = this.GetComponent <HealthScript>(); if (playerHealth != null) { playerHealth.DoDamage(1); } } }
private void OnCollisionStay2D(Collision2D collision) { if ((collision.gameObject.tag == "Player") && (damageCDTimer <= 0)) { Debug.Log("1"); damageCDTimer = damageCD; HealthScript hs = collision.gameObject.GetComponent <HealthScript>(); if (hs) { Debug.Log("2"); hs.DoDamage(Damage); } } }