OnTriggerEnter and attachedRigidbody checks

I have nested colliders in a GameObject looks exactly like this:

Player (Rigidbody - BoxCollider attached)

  • Hand (BoxCollider attached)

Now i put a FMOD Studio Event Emitter to an empty GameObject with a component of BoxCollider.
Its events set to TriggerEnter and TriggerExit.
Now when i go inside that empty GameObject, it triggers as expected but twice.

It seems it plays twice because of unity: https://forum.unity.com/threads/question-about-nested-colliders.590692/#post-3944833

// EventHandler.cs
private void OnTriggerEnter(Collider other)
{
    // lets assume that the "other" is GameObject "Hand" and its collider's attachedRigidbody is equals to Player's Rigidbody.
    if (string.IsNullOrEmpty(CollisionTag) || other.CompareTag(CollisionTag) || (other.attachedRigidbody && other.attachedRigidbody.CompareTag(CollisionTag)))
    {
        HandleGameEvent(EmitterGameEvent.TriggerEnter);
    }
}

private void OnTriggerExit(Collider other)
{
    if (string.IsNullOrEmpty(CollisionTag) || other.CompareTag(CollisionTag) || (other.attachedRigidbody && other.attachedRigidbody.CompareTag(CollisionTag)))
    {
        HandleGameEvent(EmitterGameEvent.TriggerExit);
    }
}

Should i edit that by my needs? Will it break other things?

Hi,

If you do not want it to trigger for the Hand trigger then you could add an if statement like:

// EventHandler.cs
private void OnTriggerEnter(Collider other)
{
    if (other.tag == "Hand")
        return;

This way, any object with the tag Hand will not trigger the event. I believe it will be easier to vet the things that we do not want to trigger the event rather than filtering only the events you want to play.

Let me know if that works.

1 Like