if you remove the Destroy(gameobject) you placed, the rigidbody that is receiving the explosive force is the bomb. you will see that the object (bomb) was floating after “Exploding”.
The AddExplosionForce applies a force that simulates explosion effects to the RigidBody, not to the objects around it.
For that to work, you could:
A) you could create a collider (trigger) around the bomb, and after the time hits it’s limit, it will give a explosion force to every rigidbody inside it.
B) You could use a overlap sphere and get all colliders inside and them apply force to them.
Code i did way ago:
private Collider[] hitColliders;
public float blastRadius;
public float explosionPower;
public LayerMask explosionLayers;
void OnCollisionEnter(Collision col)
{
//Debug.Log(col.contacts[0].point.ToString());
ExplosionWork(col.contacts[0].point);
Destroy(gameObject);
}
void ExplosionWork(Vector3 explosionPoint)
{
hitColliders = Physics.OverlapSphere(explosionPoint, blastRadius, explosionLayers);
foreach (Collider hitCol in hitColliders)
{
//Debug.Log(hitCol.gameObject.name);
if (hitCol.GetComponent<Rigidbody>() != null)
{
hitCol.GetComponent<Rigidbody>().AddExplosionForce(explosionPower, explosionPoint, blastRadius, 1, ForceMode.Impulse);
}
}
}
This code have its flaws, as i did it way before. You could remove the ForceMode.Impulse, or change it (see documentation for this one, it’s kinda useful all the modes it have.).
This mode i used is a bit more hard to understand, but explanations:
A) It get all the colliders that it hit.
B) Explosion Radius and power;
C) Only hit things in a determined layer (Player, terrain, objects etc.)
It’s useful, but first: Your player do have a rigidbody, if the player’s rigidbody is Kinematic, then you need to disable it to apply force and after the player hits the ground, you need to enable it again or else it will bug.
If the player is not kinematic (meaning it will only use physics when code dictates to), then the player need to have its X and Z rotation fixed, otherwise he will start spinning like crazy.
Edit: With this code, the bomb will explode after contact with the ground (OnCollisionEnter), you can change that to an update() where it will after some time, explode like normal.