- Home /
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!
Answer by save · Jun 03, 2011 at 10:30 AM
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.
$$anonymous$$ake sure that this is for 3D. In 2D, things are different
This is great and all however regardless of how you filter the objects to which you will apply the 'force', one particular thing will not change.
AddExplosionForce(power, explosionPos, radius, 3.0);
This will apply force to any object with a rigidbody within the radius of the object you filtered out. What would be nice is if you can filter the 'AddExplosionForce' function so that it only affects a specific layer. It currently affects all rigidbodies.
Answer by andrew-lukasik · Jan 31, 2013 at 06:05 PM
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
Your answer
Follow this Question
Related Questions
AddExplosionForce 0 Answers
What does rigidbodies sleeping have to do with collision detection? 0 Answers
Jump without character controller?? 2 Answers
Setting Max Angular Force For Y For Jumping 2 Answers
Rigidbody sphere slows down quickly 2 Answers