- Home /
Object only explodes when it collides with certain object, not all rigidbodies.
I have TNT that has an explosion script attached to it. I want it so that my TNT only explodes when it collides with my stockcar rather than other objects like the ground as well. I think I would usually know how to do this using tags but I used a script from a tutorial and am not sure how to put it in. I would also like to know how I would change the script so the sound plays only when the object collides with the stockcarHere is my entire script. Thanks,
var explosionRadius = 5.0;
var explosionPower = 10.0;
var explosionDamage = 100.0;
var explosionTime = 1.0;
var sound : AudioClip;
var soundVolume : float = 2.0;
function Start () {
var explosionPosition = transform.position;
var colliders : Collider[] = Physics.OverlapSphere (explosionPosition, explosionRadius);
for (var hit in colliders) {
if (hit.gameObject.name == "Stockcar") {
hit.rigidbody.AddExplosionForce(explosionPower, explosionPosition, explosionRadius, 3.0);
var closestPoint = hit.rigidbody.ClosestPointOnBounds(explosionPosition);
var distance = Vector3.Distance(closestPoint, explosionPosition);
// The hit points we apply fall decrease with distance from the hit point
var hitPoints = 1.0 - Mathf.Clamp01(distance / explosionRadius);
hitPoints *= explosionDamage;
// Tell the rigidbody or any other script attached to the hit object
// how much damage is to be applied!
hit.rigidbody.SendMessageUpwards("ApplyDamage", hitPoints, SendMessageOptions.DontRequireReceiver);
}
}
if (sound){
AudioSource.PlayClipAtPoint(sound, transform.position, soundVolume);
// stop emitting ?
if (particleEmitter) {
particleEmitter.emit = true;
yield WaitForSeconds(0.5);
particleEmitter.emit = false;
}
}
}
If you want the tnt to only explode when touching a specific object, you should probably post the code that causes the explosion, not the code that is the explosion :)
Is there a reason why you are using Physics.OverlapSphere
ins$$anonymous$$d of just using a SphereCollider
and OnTriggerEnter
? I'm actively working on my own project with Unity and for this case I would say that the 2nd approach would do just fine.
No reason, just used a script I found. I figured it out using another similar script and added as written below.
Ah I see, no problem. Good to hear you got it working!
Answer by namekaw · Apr 16, 2013 at 08:30 PM
Just needed this: if (collision.gameObject.name == "Stockcar")
Answer by robertbu · Mar 26, 2013 at 11:35 PM
You will need to run through the list first and check the tag or the name. Untested example code:
var foundCar = false;
for (var hit in colliders) {
if (hit.name == "Car") {
foundCar = true;
break;
}
}
if (!foundCar) return;
Replace "Car" with whatever you've name your car(s). Note you are currently only making the check once (in Start()). You need to either put it in Update(), or make it a function and call using InvokeRepeating(). Or make it a coroutine.
I don't know why you would be breaking the code? It doesn't seem to work or make much sense to me. I tried hit.gameObject.name == "Car", but the TNT still explodes when it hits other objects.
Post your modified code and let me take a look. "break" simply jumps out of the 'for' loop.
@Benproductions1 is right. I assume this script was the explosive and made itself explode. But if there is code elsewhere that causes this to explode, then that is what we need to see.
I have updated my script to show all of it. Now it actually does not explode when it hits anything including the car.