- Home /
OverlapSphere Limit colliders
Hi. Here's my code:
var colliders : Collider[] = Physics.OverlapSphere (transform.position, 3);
for (var hit in colliders) {
if (hit.transform.parent.tag == "EnemyShip" || hit.transform.parent.tag == "EnemyGround") {
hit.transform.parent.GetComponent("enemyShipScript").takeDamage(bulletDamage);
print("Hit!");
}
}
My problem is that it prints "Hit!" for every collider that the enemy has. My enemies have several colliders. How do I limit it so that it only applies the damage once per enemy?
Answer by koirat · Jan 31, 2013 at 05:54 PM
What I'm doing is to search for a specified component In my case it's "DamageReceiverComponent" in a hierarchy. Starting from a collider.gameObject moving up (I do not search in children). Now I have got a list of DamageReceiverComponents, I remove all duplications and apply damage to unique ones. DamageReceiverComponent.ReceiveDamage(IDamage damage);
Your answer
Follow this Question
Related Questions
Enemy Detection Area 1 Answer
Enemies fall through floor when chasing player. 1 Answer
Enemy Cube going through walls. 1 Answer
Percentage of Collider within a Trigger Area (C#) 1 Answer
Enemy Collision Wont Work! PLEASE HELP 0 Answers