- Home /
Question by
Monster5555 · May 06, 2016 at 03:42 PM ·
collisionexplosionaddexplosionforce
OverlapSphere not detecting colliders in range so AddExplosionForce can't work properly
I have a car prefab with a Rigidbody component attached to it (root element), the colliders (box colliders) are child of Chassis (child of root) something like this: Car/Chassis/Colliders. When I fire a rocket with a script attached using Physics.OverlapSphere it won't detect the colliders nor Rigidbody in the car. Any suggestion?
Here is the script attached to the rocket....
#pragma strict
var radius :float = 5;
var power : float = 10;
var explosiveLift : float = 1.0;
var explosion : GameObject;
var explSound : AudioClip;
`enter code here`function Start () {
Destroy(gameObject, 3);
}
function OnCollisionEnter (collision : Collision) {
var grenadeOrigin : Vector3 = transform.position;
var colliders : Collider[] = Physics.OverlapSphere(grenadeOrigin, radius);
for (var hit : Collider in colliders){
if (hit.GetComponent.<Rigidbody>()){
hit.transform.GetComponent.<Rigidbody>().AddExplosionForce(power, grenadeOrigin, radius, explosiveLift);
Debug.Log("Rigidbodies in area " + hit.GetComponent.<Rigidbody>().gameObject.name);
var explosion1 : GameObject;
explosion1 = Instantiate(explosion, transform.position, transform.rotation);
GetComponent.<AudioSource>().Play();
Destroy(gameObject);
Destroy(explosion1, 3);
}else {Debug.Log("No Rigidbodies in range");}
}
}
,
Comment