I’m creating a custom explosion for an FPS game (I will not be using the Detonator tool). So far it works, but there is ONLY an upward force, i.e., all objects within range of the explosion move upward, even if they are below the explosion’s epicenter. I would like the explosion to be circular, exerting forces in 360 degrees.
Here is my code so far:
var explosionRadius = 5.0;
var explosionPower = 10.0;
var explosionTimeout = 2.0;
var explosionPosition = transform.position;
var colliders : Collider[] = Physics.OverlapSphere (explosionPosition, explosionRadius);
for (var hit in colliders) {
// Calculate distance from the explosion position to the closest point on the collider
var closestPoint = hit.ClosestPointOnBounds(explosionPosition);
var distance = Vector3.Distance(closestPoint, explosionPosition);
}
// Apply explosion forces to all rigidbodies
colliders = Physics.OverlapSphere (explosionPosition, explosionRadius);
for (var hit in colliders) {
if (hit.rigidbody)
hit.rigidbody.AddExplosionForce(explosionPower, explosionPosition, 0, 3.0);
}
Destroy(gameObject, 2);
Any idea on how I can get my explosion working properly?