- Home /
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.
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? :(
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.
I would love an answer to this as well. I'm struggling to figure out how to solve this without some really janky nonsense.
Answer by CamSEgeland · Feb 27, 2020 at 11:41 AM
Did you ever find a solution or elegant workaround for getting thisCollider on OnCollisionExit?
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.
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.
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);
}
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);
}
}
}
}
}