- Home /
explosion/selecting everything inside a sphere
let's say I have an explosion.
there's a centre of explosion (Vector3, point in space) and there's a sphere around it, with certain radius.
how to detect which objects in the scene are "inside" this sphere's radius?
Answer by Eric5h5 · Dec 18, 2010 at 10:46 PM
Use Physics.OverlapSphere (as long as the objects you're interested in have colliders).
Ahhh. Didn't remember that. Good answer! BTW, what's the difference in performance overhead between that and a simple Vector.Difference?
Not sure, but I imagine OverlapSphere is faster since it's running natively.
Answer by The_r0nin · Dec 18, 2010 at 10:43 PM
Basically:
if (Vector3.Distance(enemy.transform.position,explosion.transform.position)<radiusOfSphere){
// do stuff here
}
This will measure from the pivot of the target object, so you might not get a true if its center is outside of the sphere (even though part of it might be inside). You could also instantiate a spherical object and check for collisions (if everything else has colliders). It depends on how exact you want your explosion effect...