- Home /
OverlapSphere Doesn't Return Objects Inside?
I've been experimenting with some explosion code for the past few hours and I'm trying to make an explosion that does damage to everything in a certain radius, but not through walls. I'm thinking my best bet is to use OverlapSphere to get all of the objects in range, then raycast to them to see if they're through a wall. If the raycast goes through, deal damage.
HOWEVER I have noticed that OverlapSphere is not finding things consistently, and I have no idea. why. I've tried testing things but I'm not getting anything consistent enough to debug properly. So here's my hypotheses:
1: (Least likely) OverlapSphere is biased in the positive X, and negative Y directions, and only dealing damage to things in that direction.
2: (More likely) OverlapSphere only returns colliders that intersect with the sphere itself. Thus, a collider that is completely inside of the sphere radius will not be returned. While, logically, this seems more likely, it doesn't test consistently.
I have a line of "exploding barrels" set up that, when they "TakeDamage," they instantiate an explosion that is capable of dealing damage. This functions properly - shooting a barrel makes it explode, it exploding will make other barrels explode -- inconsistently. Depending on the radius used, sometimes the barrel next to it will be skipped and the one next to that will explode, thus leading me to hypothesis 2.
Given that it is almost always the barrels on the right of the one shot that explode, and ones to the left almost NEVER explode, I have hypothesis 1.
When an explosion is instantiated, this is the code used to deal damage with the OverlapSphere:
Collider[] hitColliders = Physics.OverlapSphere (transform.position, radius);
foreach (Collider hit in hitColliders) {
if (!collidersDamaged.Contains (collider)) {
collidersDamaged.Add (collider);
hit.SendMessage ("TakeDamage", explosionDamage, SendMessageOptions.DontRequireReceiver);
}
}
Explosions are instantiated at the position of the barrel, shown here:
public void TakeDamage (int amount)
{
if (!hasBeenDestroyed) {
hasBeenDestroyed = true;
health -= amount;
if (health <= 0) {
if (explosionEffect != null)
Instantiate (explosionEffect, transform.position, transform.rotation);
GameObject explodedObject = (GameObject)Instantiate (destroyedVersion, transform.position, transform.rotation);
explodedObject.transform.position = transform.position;
Destroy (gameObject);
}
}
}
The pivot point for the barrel is in the dead center of the barrel. I have also checked the explosion position in game, and it is lining up properly with the barrel. Thus, it cannot be that the explosion is off-position.
My questions: Why is my OverlapSphere not reliable? Is there a better way to do what I'm trying to do?
It is good practive to use a Layermask in these types of situation ( when casting rays or spheres). Try setting all your enemies in an enemy Layer and then add a public mask to your overlap script and set it in to enemies in the inspector.
Just a guess: hasBeenDestroyed won't be passable after the first call, but health might still be to high for the barrel to get destroyed, maybe that's it.
And then: I just created an empty testlevel and created a bunch of 1x1 cubes, reaching from -5 to 5 on x and z axis with all 0 on the y. Doing the OverlapSphere at Vector3.zero with a radius of 5 got me all cubes except for those in the corners.
So, sorry, both 1 and 2 are incorrect and your problem comes from somewhere else.
Try debugging everything one at a time. Get yourself the names of the collider you OverlapSphered against before checking anything else or even create a Sphere for visuals with the size of the Overlap to see if it's in the correct position.
Your answer
Follow this Question
Related Questions
Physics.OverlapSphere 0 Answers
AddExplosionForce only goes up 0 Answers
overlapsphere to destroy NPCs on exit 1 Answer
Delete Explosion After 1 Second Script Not Working 2 Answers