# How can i detect if the player is inside an explosion radius?

I'm making a driving game, there are enemies which spawn on the side of road and when my player car gets close, they fire a rocket in front of the player. I need to detect if the car is within a certain radius of the explosion center. So when I can detect that I'm assuming I can then use rb.AddExplosiveForce to make the car fly a bit or something.

What is the recommended way to get the blast radius collider (or equivalent) and detect if the player is inside it?

• You can use : docs.unity3d.com/ScriptReference/Physics.OverlapSphere.html Example docs.unity3d.com/ScriptReference/… It's very basic. Commented Jul 21, 2017 at 23:46
• As an aside, "can I assign a collider to an explosion?" - well, did you try? Did anything stop you? ;) Some questions are best answered by diving in and giving it a shot, even if you don't know if it's the "right" way (we so rarely have unambiguous right ways to do things in games ;) ) Commented Jul 22, 2017 at 1:05
• @DMGregory I do agree with your comments. But from my experience I've found that if I dive in to often without reading and asking first, thats when i sometimes end up in an absolute complete mess and end up literally scrapping the project and/or some of the assets/scripts I put into it. But im quite far in to making this game so I was trying to avoid that. I was looking for the recommended way (or rather is it not recommended to put the extra collider as opposed to your sqrMagnitute solution below). The game is working nicely now and getting near complete thanks to yours and others help Commented Jul 22, 2017 at 15:56

For an instantaneous radius check, you can use the OverlapSphere method like so:

Collider[] hits = Physics.OverlapSphere(
explosionCenter,


This gives you an array containing all colliders touched by the explosion's spherical volume. You can then iterate through that array to apply forces & damage to their corresponding objects.

For better control over performance, you can use the NonAlloc version where you prepare a reusable buffer in advance, so it can fill that existing array with its results instead of allocating new ones and creating garbage to collect later.

However, if only the player is affected, then you can skip the physics queries entirely and just use a distance check to detect whether the Player object is within a given radius of the hit point:

 if((player.transform.position - explosionCenter)

• You can leave that parameter out if you don't need to filter your results to particular layers: it will catch everything by default. If you need to exclude certain layers (say you have static scenery with colliders you'd like to ignore) you can expose a public LayerMask layersToHit; field on your script — this will give you a checklist of layers in the Inspector that you can toggle on & off as needed, similar to the layer settings on lights & cameras. Commented Jul 21, 2017 at 23:54