AddExplosionForce only pushing objects in +Y

Hello. I’m having a problem with Rigidbody.AddExplosionForce where it seems to work as far as force and radius, but it only seems to push objects in the +Y axis.

I’m working on a space shooter game, where whenever a bullet hits a ship, the bullet prefab is destroyed and a new explosion prefab is created. The explosion prefab has a particle effect and a script that uses AddExplosionForce to push away all other rigidbodies in range.

And effectively it pushes them, but not away from the impact point, only upwards. The code is the exact example used on the Unity Script Documentation for the method, yet it only pushes others in +Y.

The game is meant as 2D, so objects only move in X and Y. Yet I’m not artificially fixing the Z velocity to 0, only being careful never to increase force in that direction. I set constraints on the rigidbody, but I don’t know if either of these is part of the problem.

Any ideas? Thanks for any help.

I’d check the upwards modifier. For reference, the script is below:

var radius = 5.0;
var power = 10.0;
function Start () {
     // Applies an explosion force to all nearby rigidbodies
     var explosionPos : Vector3 = transform.position;
     var colliders : Collider[] = Physics.OverlapSphere (explosionPos, radius);

     for (var hit : Collider in colliders) {
          if (!hit)
          continue;

          if (hit.rigidbody)
          hit.rigidbody.AddExplosionForce(power, explosionPos, radius, 3.0);
     }
}

In the example, there’s an upwards modifier of 3 (the last argument). Try reducing that to 0 and see what happens.

In my case the issue was that the distance between the center of the explosion and the radius of the collider of the rigidbody the explosion force was applied to was too small, letting the explosion happen INSIDE the collider. This had the effect of the rigibody always thrown in Y direction.

So, Unity actually force an upward value only for direction in AddExplosionForce. It become the most apparent at the contact point very close to the explosion position.
Also, if you set a value of 0.0f as Upward, Unity will actually change it as 1.0f for you. It’s nice like that.

Personally, I find it good enough for pushing the body away from the explosion. I have upward value as 0.01f.

But I have a case where my radius is 0.0f, then I do a special case, which you could do yourself otherwise sadly.

I have a physic utility function where I do this

[Missing code here]
// F = m * a
// The velocity here of the rigid body is consider to instantly decrease to 0 by touching a collision
// Therefore we transform the velocity into acceleration (being a deceleration)
float explosionForce = rigidBodyExplosionFrom.velocity.magnitude * rigidBodyExplosionFrom.mass;
if (radius == 0.0f)
{
    targetRigidBody.AddForceAtPosition(rigidBodyExplosionFrom.velocity.normalized * explosionForce, explosionPosition, forceMode);
}
else
{
    targetRigidBody.AddExplosionForce(explosionForce, explosionPosition, radius, upwardModifier, forceMode);
}

Since it’s an explosion I use ForceMode.Impulse

I had almost the same problem, AddForceAtPosition moved my player prefab only by Y-axis. I disabled “Apply root motion” flag in Animator component, and it solves my problem.

I had the same problem. When I added a forcemode, it did the trick!

Collider[] colliders = Physics.OverlapSphere(this.transform.position, checkRadius);
foreach (Collider myhit in colliders) {
    Rigidbody rb = myhit.GetComponent<Rigidbody>();
    if (rb != null) {
        rb.WakeUp();
        rb.isKinematic = false;
        rb.velocity = Vector3.zero;  //This makes physics force consistent
        rb.AddExplosionForce(blastPower, this.transform.position, blastRadius, upForce, ForceMode.Force);
        Debug.Log("KABOOM! KABOOM!");
}

}

I chose ridiculous values initially that I set in the inspector, and they were:

blastPower = 10000, blastRadius = 100, upForce = 0

For more information on the differences between the forcemodes, I found this thread to be helpful!
https://answers.unity.com/questions/696068/difference-between-forcemodeforceaccelerationimpul.html?childToView=1740080#comment-1740080