- Home /
Rigidbody.AddExplosionForce() Not Playing Nice With Trigger Colliders
I have an object with a 1x1x1 BoxCollider and a Rigidbody with a mass of 1. It also has a child object with a SphereCollider of radius 50 and IsTrigger set to true. For some reason, when I call Rigidbody.AddExplosionForce() on this object, it gets blasted away as if by an insanely powerful force, even when the explosion force is neither very strong nor very close by. The larger I make the child SphereCollider's radius, the greater the effect. Curiously, I do not see this effect when I use Rigidbody.AddForce(). I reported my findings in this SO post, thinking them to be a bug in the physics engine, but the comments there led me to believe that everything was behaving as expected. So here's my question: how can I use Rigidbody.AddExplosionForce() on a GameObject with a large trigger somewhere in its hierarchy, without that trigger effecting it's physics?
EDIT: Here is my test explosion script, copied from the linked SO post:
using UnityEngine;
public class Exploder : MonoBehaviour {
// HIDDEN FIELDS
private float _highY = 0f;
private bool _moving;
// INSPECTOR FIELDS
public Rigidbody Target;
public float ExplosionForce;
public float ExplosionRadius;
public float UpwardsModifier;
// EVENT HANDLERS
private void Update() {
// Explode on click
bool clicked = Input.GetMouseButton(0);
if (clicked) {
if (Target != null)
Target.AddExplosionForce(ExplosionForce, transform.position, ExplosionRadius, UpwardsModifier, ForceMode.Impulse);
}
// Report the object's highest y-level
if (Target.velocity.sqrMagnitude > 0) {
_highY = Mathf.Max(_highY, Target.transform.position.y);
_moving = true;
}
else if (_moving) {
Debug.LogFormat("Highest Y: {0}", _highY);
_highY = 0f;
_moving = false;
}
}
}
Answer by meat5000 · May 09, 2016 at 11:44 AM
If you look in the docs at the given example
http://docs.unity3d.com/ScriptReference/Rigidbody.AddExplosionForce.html
you see that they use OverlapSphere to locate the Colliders before applying the Explosion to each Collider. I do not think that OverlapSphere will detect triggers.
Use the returned collider array to apply your explosions. As you miss this step you are effectively applying the explosion to all rigidbodies despite the collider status.
I mean, remove all colldiers from the object and see if the explosion still applies (whilst its falling through space). See what happens.
EDIT: Add a Kinematic Rigidbody without Gravity to the EMPTY child object. BOOM. Instant fix.
@meat5000 Thanks for your response! OverlapSphere() can be configured to detect triggers or not, same as any raycast, using the QueryTriggerInteraction enum and project Physics settings. This is not my issue however, as Rigidbody.AddExplosionForce() is applied to a single Rigidbody, and raycasting is not required. The linked SO post contains the code for a script that I wrote to test all this, and I will add it to my original post here. As you can see, it affects only a single Rigidbody (set in the Inspector), but the explosion force still seems to be drastically scaled by the size of a trigger Collider anywhere in that Rigidbody's object's children. Any ideas on how to get around that?
So does this also happen if you tell the physics engine to ignore collisions between the two colliders on the object?
//JS
var colsOnObject : Collider[];
function Start ()
{
colsOnObject = transform.GetComponents.<BoxCollider>();
Physics.IgnoreCollision(colsOnObject[0], colsOnObject[1]);
}
(I know you use different colliders, this is just an example)
Yeah I saw the script already :P I must admit Ive tried recreating this problem with Box Colliders and I am not able to.
$$anonymous$$ake sure that the Physics Collider is ABOVE the Trigger in the Component list, in inspector.
Unfortunately, Physics.IgnoreCollision doesn't seem to fix things (on a side note, I haven't used that function before and it looks pretty handy, thanks for mentioning it!) I'm really surprised you're not seeing this problem. Again, I've got an object with a BoxCollider/Rigidbody, and a child object with a trigger SphereCollider. I just tried using a BoxCollider for the child trigger collider ins$$anonymous$$d, and I'm still getting very large explosion forces. $$anonymous$$y Exploder values are ExplosionForce = 2, ExplosionRadius = 3, and Upwards$$anonymous$$odifier = 2 and I have it positioned 2 units directly beneath the collider object. This setup doesn't replicate the problem for you?
Yes I recreated it with that setup. Edited my answer.
Wow, that is a simple freakin fix! I was actually able to work around this by implementing my own AddExplosionForce() code that calculated the force magnitudes/directions and used Rigidbody.AddForce(), but you solution is much simpler. The fact that adding a kinematic Rigidbody is even necessary, and that AddForce() is unaffected by the trigger collider's size while AddExplosionForce() is, still leads me to believe that there is a bug in the physics engine. Either way, your answer is just the solution I needed. Thanks for your thoroughness!
Your answer
Follow this Question
Related Questions
Move rigid body using explosion force in C# 0 Answers
Launching a player in a certain direction - C# 1 Answer
Earth quake with physics? 1 Answer
OnTriggerEnter triggers twice when changing the parent of a gameObject inside of a collider. 1 Answer
Is it possible to combine child trigger of a object to only trigger OnTriggerEnter once? 1 Answer