Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by Rabadash8820 · May 09, 2016 at 11:41 AM · physicsrigidbodytriggersexplosion

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;
     }
 }
 }

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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.

Comment
Add comment · Show 6 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Rabadash8820 · May 09, 2016 at 06:10 PM 0
Share

@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?

avatar image meat5000 ♦ Rabadash8820 · May 09, 2016 at 06:36 PM 0
Share

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)

avatar image meat5000 ♦ meat5000 ♦ · May 09, 2016 at 06:44 PM 0
Share

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.

avatar image Rabadash8820 · May 09, 2016 at 07:02 PM 0
Share

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?

avatar image meat5000 ♦ Rabadash8820 · May 10, 2016 at 12:05 PM 0
Share

Yes I recreated it with that setup. Edited my answer.

avatar image Rabadash8820 · May 10, 2016 at 06:38 PM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges