- Home /
Overlapsphere not returning all colliders it touches
I'm using an overlapsphere to detect whether enemies are close enough to be hit, iterating through the array it generates, and applying damage to every enemy within range. The problem is, enemies only take damage inconsistently: I've put three enemies in a cluster around me, make the overlapsphere more than big enough to cover them all, and watched as only one or two of the pack take damage. Am I somehow missing something in my iteration?
Below is the function I'm trying to fix:
void Attack(){
center = transform.position;
Collider[] hitColliders = Physics.OverlapSphere (center, radius);
for (int i = 0; i < hitColliders.Length; i++){
if (hitColliders[i].gameObject.tag == "Enemy"){
distanceToEnemy = Vector3.Distance(transform.position, hitColliders[i].gameObject.transform.position);
if (distanceToEnemy <= attackRange){
hitColliders [i].gameObject.SendMessage ("ApplyDamage", damage);
Debug.Log ("Applied damage");
}
}
i++;
}
}
Derp question, but needs asking: You made sure they are all inside "attackRange" as well as "radius", right?
Is there a reason you have a radius and a range?
Good question to ask, and I did- I have a gizmo which draws a sphere to the same dimensions as my overlapsphere, and to test this I set range to an absurdly high number, verified they were both in the sphere and in range with a drawline, and it still didn't consistently pick up every enemy whose collider was in range.
I have both because I want to make sure range is being calculated based on the enemy model's position, not their collider position. Later on I might end up using colliders as vision/sensory cones, for instance, and having this set up in advance lets me avoid punching anything that can see me.
I've cleaned up and improved my code a bit, current version below:
void Attack(){
anim.SetTrigger("Punch");
Collider[] hitColliders = Physics.OverlapSphere (transform.position, radius);
for (int i = 0; i < hitColliders.Length; i++){
if (hitColliders[i].gameObject.tag == "Enemy"){
distanceToEnemy = Vector3.Distance(transform.position + new Vector3 (0,1,0), hitColliders[i].gameObject.transform.position);
directionToTarget = (hitColliders[i].gameObject.transform.position - transform.position).normalized;
if (distanceToEnemy <= attackRange && (Vector3.Dot(directionToTarget, transform.forward) > 0.85)){
hitColliders [i].gameObject.Send$$anonymous$$essage ("ApplyDamage", damage);
Debug.Log ("Applied damage");
}
}
i++;
}
}
Oddly enough the same problem persists- very occasionally, one enemy will not detect the attack, and repositioning myself doesn't change things- the only way to make him register the hit is to step back until he's outside the sphere, and then re-approach, at which point he takes damage.
Answer by Nick4 · Jun 11, 2014 at 07:36 AM
I think it's because you're increasing i variable twice.
facepalm I don't know how I didn't catch that before, but you just made my day! Thank you :)
Your answer
Follow this Question
Related Questions
Want to spawn a box on other box but don't want those boxes to have same tag on each other using tag 0 Answers
Distribute terrain in zones 3 Answers
Unity C# Bomb Radius Issue 0 Answers
Multiple Cars not working 1 Answer
OverlapSphere ignoring all colliders when I use the layerMask parameter. 1 Answer