Rigidbody.AddExplosionForce Is Not Working

I copy and pasted the unity API on
Rigidbody.AddExplosionForce
and it didn’t work, is their anything i have to do to my scene to prep it?

using UnityEngine;
using System.Collections;

// Applies an explosion force to all nearby rigidbodies
public class TestExplosion : MonoBehaviour {
    public float radius = 5.0F;
    public float power = 10.0F;
   
    void Start() {
        Debug.Log ("bang");
        Vector3 explosionPos = transform.position;
        Collider[] colliders = Physics.OverlapSphere(explosionPos, radius);
        foreach (Collider hit in colliders) {
            Rigidbody rb = hit.GetComponent<Rigidbody>();
           
            if (rb != null)
                rb.AddExplosionForce(power, explosionPos, radius, 3.0F);
           
        }
    }
}

Confirm your objects actually have a Rigidbody component on them. Change Power to 300.

OK, that the problem, i was using numbers that were way to small to do anything. after changing the power to 300 it moved the block. thank you.

I wanted to get the explosion after a collision and it doesn’t work, what do I have to do?
(I also tried changing power to 300 but keeps not working)

public class RomperVidrio : MonoBehaviour
{
    public Transform brokenObject;
    public float magnitudeCol, radius, power;

    void OnCollisionEnter(Collision collision)
    {
        if (collision.relativeVelocity.magnitude > magnitudeCol)
        {
            Destroy(gameObject);
            Instantiate(brokenObject, transform.position, transform.rotation);
            brokenObject.localScale = transform.localScale;
            Vector3 explosionPos = transform.position;
            Collider[] colliders = Physics.OverlapSphere (explosionPos, radius);

            foreach (Collider hit in colliders){
                Rigidbody rb = hit.GetComponent<Rigidbody>();
                if (rb != null)
                    rb.AddExplosionForce(power, explosionPos, radius, 3.0f);
     

            }
             

        }
    }
}

Did you already try LaneFox’s suggestions posted here 4 years ago? Because an explosive force of 3 is probably going to result in a similar force to a firecracker going off under a bus.

2 Likes

Yep mate, I tried, but it doesn’t want to do the thing

Not working for mee too…any updates??

1 Like

Use Bigger Numbers! I needed 1000 Power and 100 Upward Force…
Make sure your Rigidbody is NOT Kinematic

1 Like

I have a similar problem, I am trying to launch my player, but it doesn’t get knocked back at all, he has a Rigidbody, capsule collider, and player controller, the knock back number is 1500. it is not kinemetric either

For anyone coming across this, remember that the force applied to the Rigidbody will be relative to its mass. For an object with a mass of 100, it may need an explosion force of 100.000.

Since the topic was already bumped. The original code may have a slight flaw. It’s quite common for rigidbodies to have compound collider. That means it may have several child objects which colliders which do belong to the rigidbody on the parent. A collider generally belongs to the rigidbody either on the same gameobject or to the rigidbody on the closest parent. Unity has a special method to get the “closest” component in a certain direction. So instead of GetComponent you want to use GetComponentInParent. This method does the same as GetComponent, but when that component is not found on that gameobject, it will walk up the hierarchy and look for that component on the parent object,

GetComponentInChildren does the same in the other direction. Though since OverlapSphere gives us just the collider that overlap, we want to find the rigidbody that the collider belongs to, so GetComponentInParent would reliably find the correct gameobject,

As others have already mentioned, only rigidbodies which are NOT kinematic can actually be affected by forces. So kinematic rigidbodies will ignore any kind of forces.

Yes, ordinary forces will get devided by the mass of the object the force is applied to. That’s just newtonian physics (F=ma → a = F/m). About 10 years ago I wrote down how the different force modes will affect the velocity of the rigidbody The AddExplosionForce method also has the optional ForceMode parameter and be default it’s set to Force. However a normal force is meant to be applied every phyiscs frame. That’s how forces work. When you want to apply a single on-time push, you should use ForceMode.Impulse

While forces are divided by the mass to get an acceleration, they are also multiplied by deltaTime. Impulse “forces” are not really forces but apply a certain amount of energy at once