3
\$\begingroup\$

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?

\$\endgroup\$
3
  • 1
    \$\begingroup\$ You can use : docs.unity3d.com/ScriptReference/Physics.OverlapSphere.html Example docs.unity3d.com/ScriptReference/… It's very basic. \$\endgroup\$
    – Sidar
    Jul 21, 2017 at 23:46
  • 1
    \$\begingroup\$ 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 ;) ) \$\endgroup\$
    – DMGregory
    Jul 22, 2017 at 1:05
  • \$\begingroup\$ @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 \$\endgroup\$ Jul 22, 2017 at 15:56

1 Answer 1

4
\$\begingroup\$

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

Collider[] hits = Physics.OverlapSphere(
          explosionCenter,
          explosionRadius,
          layerMaskToCheck );

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)
   .sqrMagnitude < explosionRadius * explosionRadius)

(Here using a common trick of comparing squared values to skip an unnecessary square root operation — this isn't terribly important if you're only doing it a few times, just a habit a lot of us are in)

\$\endgroup\$
10
  • 1
    \$\begingroup\$ thank you for your detailed answer my friend. im confused by LayerMaskToCheck but I may well understand it once i get going and once i have a read of the docs. I'll give this a try right now :) \$\endgroup\$ Jul 21, 2017 at 23:50
  • 1
    \$\begingroup\$ 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. \$\endgroup\$
    – DMGregory
    Jul 21, 2017 at 23:54
  • \$\begingroup\$ ahh awesome, so i will need this method later when i start to create a more complex and object filled world. But your solution at the bottom seems great as I am only letting my players car be affected by the explosions. I'm still just trying to get my head around where exactly to use it in my code but thanks to your answer im hoping ill have it covered soon \$\endgroup\$ Jul 21, 2017 at 23:59
  • 1
    \$\begingroup\$ It would probably go in the OnTriggerEnter or OnCollisionEnter routine of your rocket (depending on whether it's using a trigger collider), so it gets called when the rocket strikes a solid surface. \$\endgroup\$
    – DMGregory
    Jul 22, 2017 at 0:01
  • 1
    \$\begingroup\$ Then there's your answer: when you instantiate the explosion, perform the radius check to see if the player is inside. Yes, hitPoint is the center of the explosion — sorry, I'm on my phone and didn't spot that I'd changed nomenclature partway through — fixed now. The magnitude we're talking about here is the distance between the explosion center and the player's position. It's not a speed or anything you need to derive beyond the formula given above. \$\endgroup\$
    – DMGregory
    Jul 22, 2017 at 0:08

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .