I'd like to modify this script to only effect an object tagged as "target". Any ideas? Thanks, Greg Code (csharp): var radius = 30.0; var power = -90.0; function Update () { // Applies an explosion force to all nearby rigidbodies var explosionPos = transform.position; var colliders : Collider[] = Physics.OverlapSphere (explosionPos, radius); for (var hit in colliders) { if (!hit) continue; if (hit.rigidbody) { hit.rigidbody.AddExplosionForce(power, explosionPos, radius, 0.0); } } }
That would be the slow way to do it. If you are able to use layers, OverlapSphere can take a layerMask, which will return anything hit in the mask. The docs are flawed and don't make this obvious. It looks like the default is all layers if you don't provide this. Even if you DO need to use tags for some reason, you can still cut back on the number of objects processed by using a more general layer setup.
Thanks for your help, guys. I went with: Code (csharp): if (hit.collider.gameObject.tag == "target") This does exactly what I want. Here is the final code I am using to attract tagged, rigidbody objects: Code (csharp): var radius = 30.0; var power = -90.0; function Update () { // Applies an explosion force to all nearby rigidbodies var explosionPos = transform.position; var colliders : Collider[] = Physics.OverlapSphere (explosionPos, radius); for (var hit in colliders) { if (!hit) continue; if (hit.collider.gameObject.tag == "target") { hit.rigidbody.AddExplosionForce(power, explosionPos, radius, 0.0); } } }