- Home /
Physics.OverlapSphere
Does anybody know, how this works. I need an array with all colliders touching or inside the sphere. Everything I found just this http://unity3d.com/support/documentation/ScriptReference/Physics.OverlapSphere.html please.help!
Answer by aldonaletto · Jan 09, 2012 at 11:31 PM
That's exactly what you want, and the example is found in AddExplosionForce (ok, it should be in OverlapSphere as well...).
OverlapSphere(position, radius[, layerMask]) creates an imaginary sphere centered at position and with radius radius, and returns an array with all the colliders touching or inside the sphere. Only the colliders in the layers enabled by layerMask are returned (enables all layers if layerMask omitted). The example code is the following (should be attached to a explosion prefab):
var radius = 5.0; var power = 10.0; function Start () { // Applies an explosion force to all nearby rigidbodies var explosionPos : Vector3 = transform.position; // get all colliders touching or inside the imaginary sphere: var colliders : Collider[] = Physics.OverlapSphere (explosionPos, radius); for (var hit : Collider in colliders) { if (!hit) continue; // avoid null references (should not occur, but...) if (hit.rigidbody){ hit.rigidbody.AddExplosionForce(power, explosionPos, radius, 3.0); } } }
Answer by koirat · Jul 29, 2012 at 04:39 PM
In unity documentation Script reference about Physics.OverlapSphere there is this NOTE:
NOTE: Currently this only checks against the bounding volumes of the colliders not against the actual colliders.
This function is a joke. It's name is misleading.
So I'm asking the question is there a way to tell if my static colliders are inside a sphere. Primitive colliders would be enought, let not be to demanding.
Well, this isn't really an answer to the question. Do you actually need to know which colliders are completely inside or if they overlap? Completely inside isn't possible at all. This would be very complex.
Unfortunately you can't test collisions manually, so OverlapSphere is the best built-in solution. It you want to improve the accuracy, you have to do some manual collision checks. The easiest one would be a sphere collider, since you can simply check the distance againse the two radii. All other collder shapes are not that easy. For a box collider you might need to test each surface
Well it was not exactly the answer. It was more like a bump to the question that was answered giving a bad impresion that this function is actually doing what it's supposed to.
What I want to do is exactly like the explosion example, I don't ned to know if collider is completely inside the sphere, just to overlap is ok. As I said before it does not have to be a mesh collider, I know how difficult it is to check for a collision in concave colliders.
But seriously not checking for a Prmitive Colliders like spheres , cylinders, boxes, capsules in my opinion is a big flaw.
Unity is not a "low level" engine it should have built in all combinations of overlap/collision checks for primitive colliders.
And just checking for a bounding box (I guess it's Axis aligned) will give a very bad precision.
Your answer
Follow this Question
Related Questions
overlap half sphere? 0 Answers
How is OverlapSphere used? 2 Answers
Closest target acquisition using Physics Overlap Sphere script not working 1 Answer
OverlapSphere is really confusing 0 Answers
Sphere Overlap EachOther 0 Answers