Search Unity

Child OnTriggerEnter Confusion

Discussion in 'Scripting' started by Nigey, Mar 13, 2019.

  1. Nigey

    Nigey

    Joined:
    Sep 29, 2013
    Posts:
    1,129
    Pretty Simple. I have this:

    GameObject (Parent)
    Parent->RigidBody (using gravity)
    Parent->BoxCollider (trigger)
    Parent->MonoBehaviour (implementing magic method OnTriggerEnter)

    GameObject (Child)
    Parent.Child->RigidBody (kinematic)
    Parent.Child->SphereCollider (trigger)
    Parent.Child->MonoBehavaiour (implementing magic method OnTriggerEnter)

    No matter what, only the MonoBehaviour attaching to the parent is having the OnTriggerEnter method called. I know the engine has intentional child collider to handle concave, but as I remember when you put a RigidBody on a child, that child's collider will no longer be considered part of the parent's collective shape.

    Is this still correct? Can anyone imagine a situation where this would no longer apply?

    FYI this is the script. It's on both to see what's happening:

    Code (CSharp):
    1. public class PlayerTriggerVolume : MonoBehaviour
    2. {
    3.     public UnityEvent OnPlayerEnter;
    4.     public UnityEvent OnPlayerExit;
    5.  
    6.     private void OnTriggerEnter(Collider other)
    7.     {
    8.         Debug.LogFormat("<color=blue>{0} has been hit by {1}.</color>", gameObject.name, other.tag);
    9.  
    10.         if (other.CompareTag("Player"))
    11.         {
    12.             OnPlayerEnter?.Invoke();
    13.         }
    14.     }
    15.  
    16.     private void OnTriggerExit(Collider other)
    17.     {
    18.         if (other.CompareTag("Player"))
    19.         {
    20.             OnPlayerExit?.Invoke();
    21.         }
    22.     }
    23. }
     
  2. dgoyette

    dgoyette

    Joined:
    Jul 1, 2016
    Posts:
    4,196
    From the documentation (https://docs.unity3d.com/Manual/CollidersOverview.html):

    So, if you're going with the child collider approach, it sounds like you'll want to get rid of the child rigidbody.
     
  3. Nigey

    Nigey

    Joined:
    Sep 29, 2013
    Posts:
    1,129
    FYI I found the issue. I am using NewtonVR, who register every interactable object as they're added to the scene. It does a lookup of all child colliders, and because the parent is a networked object, and the child is non-networked, it was coming up later in the list, and so the parent was getting picked from the register first.

    So, in short. It's the code's issue. Nothing to do with the engine. Anyone else staring at this. The rules are the same as I described. Check your code, check the prefab/scene hierarchy and setup.