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!
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); } } }
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.