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
3
Question by sh_code · Jan 13, 2019 at 07:18 PM · collisioncollidercompound collidercollision event

Compound collider OnCollisionExit - how to find out WHICH sub-collider fired it?

So, I have a parent object with Rigidbody, which contains child objects with primitive colliders and no other rigidbodies.

My script on the parent object contains OnCollisionEnter and OnCollisionExit.

In OnCollisionEnter I do collision.contacts[0].thisCollider to find out which primitive collider of the compound fired the event, and that's fine.

But in OnCollisionExit, naturally I can't do this, because contacts array is empty. So: How the hell am I supposed to find out which primitive sub-collider of all of those that form the compound collider, fired the CollisionExit event?

This should be trivial, or am I insane? It should be the most basic thing to do, to find out which sub-collider of a compound collider just exited collision, but I see no way to do it.

...no, thank you, I'm not going to be making each of those into separate rigidbody attached to the whole by a joint, that's stupid and unnecessary, I don't need and want anything to do with that. Please, give me a normal, sane way to do this normal and trivial thing, thank you.

Comment
Add comment · Show 3
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 sh_code · Jan 13, 2019 at 07:20 PM 0
Share

P.S.: the fact itself that the collision.contacts is cleared BEFORE the OnCollisionExit runs, this itself is in my opinion utterly stupid, and renders large part of what OnCollisionExit should be used for completely meaningless and nonfunctional. why is the function even pretending to give me useful data, if it doesn't do so? :(

avatar image sh_code · Jan 13, 2019 at 07:21 PM 0
Share

P.P.S.: why is the post editor here on answers.unity.com ignoring my empty lines? It turns my nice paragraphs into a horrible wall of text.

avatar image Ferretanica · Apr 09, 2020 at 08:41 PM 0
Share

I would love an answer to this as well. I'm struggling to figure out how to solve this without some really janky nonsense.

4 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by CamSEgeland · Feb 27, 2020 at 11:41 AM

Did you ever find a solution or elegant workaround for getting thisCollider on OnCollisionExit?

Comment
Add comment · 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
0

Answer by Ferretanica · Apr 09, 2020 at 10:16 PM

I would love an answer to this as well. I'm struggling to figure out how to solve this without some really janky nonsense.

Comment
Add comment · 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
0

Answer by JxWolfe · Apr 09, 2020 at 10:27 PM

Unity doesn't have an easy way of doing this. The best way would to have children each with their own collider. They all have a script:

 OnTriggerEnter(Collider other) {
   GetComonentInParent<YourScript>().Enter(other, gameObject);
 }

that calls the parent function:

 public void Enter(Collider other, GameObject child) {
   //child == collider
   //you can use child.name to identify it
  
   //other is who collided. 
 }

And that could run whatever you needed it to.

Comment
Add comment · 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
0

Answer by DevinW · Oct 24, 2021 at 06:10 AM

 public class TriggerListener : MonoBehaviour 
 {
          public Action<Collider> OnEnter = delegate { };
          public Action<Collider> OnExit = delegate { };
 
          private void OnTriggerEnter(Collider collider) => OnEnter(collider);
          private void OnTriggerExit(Collider collider) => OnExit(collider);
 }
Comment
Add comment · Show 1 · 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 DevinW · Oct 24, 2021 at 06:18 AM 0
Share

If you wanted to know when a compound collider has completely exited a trigger...

 public class CompoundColliderListener : MonoBehaviour
 {
     private readonly Dictionary<Rigidbody, List<Collider>> _trackedBodies 
                      = new Dictionary<Rigidbody, List<Collider>>();
     public Action<Rigidbody> OnEnter = delegate {};
     public Action<Rigidbody> OnExit = delegate {};

     private void OnTriggerEnter(Collider other)
     {
         var rigidbody1 = other.attachedRigidbody;
         if (rigidbody1)
         {
             if (_trackedBodies.ContainsKey(rigidbody1))
             {
                 _trackedBodies[rigidbody1].Add(other);
             }
             else
             {
                 _trackedBodies[rigidbody1] = new List<Collider>() { other };
                  OnEnter(rigidbody1);
             }
         }
     }
 
     private void OnTriggerExit(Collider other)
     {
         var rigidbody1 = other.attachedRigidbody;
         if (rigidbody1)
         {
             if (_trackedBodies.ContainsKey(rigidbody1))
             {
                 _trackedBodies[rigidbody1].Remove(other);
                 if (_trackedBodies[rigidbody1].Count == 0)
                 {
                     _trackedBodies.Remove(rigidbody1);
                     OnExit(rigidbody1);
                 }
              }
         }
     }
 }


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

169 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Some questions about Concave Collider 0 Answers

Ignore child intercollision, not parent-parent 0 Answers

Compound collider object behaving as a whole? 1 Answer

making bullets disappear when they collide 3 Answers

How to detect a character is leaving a 3D world? 2 Answers


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