Search Unity

Question Child Object OnTriggerEvent activated by parent Character Controller

Discussion in 'Scripting' started by gjfnasjdnasklnd, Jun 28, 2020.

  1. gjfnasjdnasklnd

    gjfnasjdnasklnd

    Joined:
    Jun 15, 2020
    Posts:
    8
    How do I stop the parent character capsule from activating it's child triggers?

    I have a box collider that is a trigger. OnTriggerEnter gets activated by it's parent character controller and I can;t seem to get this ignored. If I use IgnoreLayerCollision (tried all parent and child layer combinations), or IgnoreCollision, OnTriggerEnter is never called.

    Code (CSharp):
    1. public class LowerCollisionScript : MonoBehaviour
    2. {
    3.     public static String otherTag = "Untagged";
    4.  
    5.     void Start()
    6.     {
    7.         //Physics.IgnoreLayerCollision(31, 31);
    8.         //or
    9.         //Physics.IgnoreCollision(GetComponentInParent<CharacterController>(), GameObject.Find("LowerCollisionObject").GetComponent<BoxCollider>());
    10.     }
    11.  
    12.     private void OnTriggerEnter(Collider other)
    13.     {
    14.    
    15.             Debug.Log("lower collision entry"); //none of these are ever logged if using either IgnoreCollision
    16.             Debug.Log("lower collider name: " + other.name);
    17.             otherTag = other.gameObject.tag;
    18.     }
    19.  
    20.     private void OnTriggerExit(Collider other)
    21.          {
    22.              Debug.Log("lower collision exit");
    23.              otherTag = "Scale";
    24.          }
    25. }
     
    Last edited: Jun 29, 2020
  2. gjfnasjdnasklnd

    gjfnasjdnasklnd

    Joined:
    Jun 15, 2020
    Posts:
    8
    [Solution]

    Added a rigidbody (kinematic enabled) component to the parent Character Controller object and all trigger events execute as described in docs. The parent object is no longer activating its child trigger events so theres no need to counter that with IgnoreCollisions or other methods.

    I did this because:
    https://docs.unity3d.com/Manual/CollidersOverview.html
    Compound colliders
    "When you create a compound collider like this, you should only use one Rigidbody component, placed on the root GameObject in the hierarchy."