Applying explosion force to a single layer?

Hi all,

I have a feeling this is a really basic question, cause nobody else is asking it, but is there a way (using Rigidbody.AddExplosionForce) to apply an explosion force that will only effect a specific layer?

For context, I have an arcade game where when an enemy dies they break apart and I apply a small force in the center to make them blow apart nicely. However I see that the lasers that my player fires are sometimes affected by these and start spinning annoyingly.

Thanks for your help!

Use tags instead (which is intended for the physics, layers are mainly intended for camera culling). It also helps if you post a little bit of code so we quickly can modify. Although here’s an example of what you can do:

Firstly set a specific tag for the things you want to be affected by the explosion force.

Secondly add a statement of where you check what tag the object has, as an example here’s a modified version of the Rigidbody.AddExplosionForce in the docs:

var radius = 5.0;
var power = 10.0;
function Start () {
    // Applies an explosion force to all nearby rigidbodies
    var explosionPos : Vector3 = transform.position;
    var colliders : Collider[] = Physics.OverlapSphere (explosionPos, radius);
    
    for (var hit : Collider in colliders) {

        //Here the colliders with tag PhysAffected will be affected by the force
        if (!hit && hit.collider.tag=="PhysAffected")
            continue;
        
        if (hit.rigidbody)
            hit.rigidbody.AddExplosionForce(power, explosionPos, radius, 3.0);
    }
}

You could of course reverse this to check if the hit collider tag is the laser so it doesn’t apply for that specific object.

After doing some google-searches I found out that you need to write layer number like this:

var colliders : Collider[] = Physics.OverlapSphere (transform.position, radius, (1 << layerMaskAsInt));

more here