Search Unity

  1. Looking for a job or to hire someone for a project? Check out the re-opened job forums.
    Dismiss Notice
  2. Unity 2020 LTS & Unity 2021.1 have been released.
    Dismiss Notice
  3. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice

OnTriggerEnter and OnControllerColliderHit produce different results

Discussion in 'Editor & General Support' started by cacotigon, Oct 28, 2019.

  1. cacotigon

    cacotigon

    Joined:
    Jun 12, 2015
    Posts:
    9
    There seems to be a possible bug in terms of collision detection depending on whether one is using a Box Collider as a trigger, versus detecting the collision via OnControllerColliderHit. Using the same box collider in conjunction with the character controller results in two very different positions in terms of collisions.

    The following test consists of a CharacterController game object, and a primitive cube whose Box Collider has been increased along the Y axis.

    In each animation, the character controller stops moving at the point of collision detection.

    Here is OnTriggerEnter:





    I can't understand why the capsule collider seems to have to "penetrate" so deeply into the box collider before OnTriggerEnter gets triggered. You can see how far the capsule collider pushes into the box collider before it triggers.

    And below we're using OnControllerColliderHit:



    Here OnControllerColliderHit in conjunction with CharacterController.Move seems to detect the collision at the actual proper collider boundaries. Is this just a huge bug in the character controller? Note that Skin Width in both attempts is set to the absolute minimum possible (0.0001) though I've tried every range of possible values.
     
  2. dgoyette

    dgoyette

    Joined:
    Jul 1, 2016
    Posts:
    3,679
    Does each "frame" in your GIFs represent one physics timestep / FixedUpdate? I ask because it's a known trait that trigger colliders don't fire their OnTriggerEnter events on the first physics frame that the collider overlaps. The event won't fire until the next FixedUpdate, assuming the two objects are still overlapping at that time. I made a post about this a while ago, and had the situation explained to me: https://forum.unity.com/threads/ontriggerenter-called-one-frame-after-collision-occurs.717164/

    Non-triggers fire OnCollisionEnter the exact frame the collision occurs, which would explain your second GIF.
     
  3. cacotigon

    cacotigon

    Joined:
    Jun 12, 2015
    Posts:
    9
    Since I'm not using any physics objects (rigidbodies) I'm doing the controller Move function in the regular Update(). As I understand it, the Character Controller does not make use of physics at all so this shouldn't be the issue, but just to make sure I dialed down the fixed timestep to 0.002 (500 fps) and still had the same issue. It's always in the exact same spot as well, almost as if the built-in character controller capsule collider is different.
     
  4. dgoyette

    dgoyette

    Joined:
    Jul 1, 2016
    Posts:
    3,679
    Are you sure about that? Maybe your character has a kinematic rigidbody on it or something? I ask because my understanding is that triggers simply don't fire at all unless there's at least one rigidbody on either the trigger collider, or the thing triggering it. (See the matrix at the bottom of this page: https://docs.unity3d.com/Manual/CollidersOverview.html)

    So my assumption is you must have a rigidbody on the character, or you'd never get any trigger events firing.

    Towards the bottom of the box in both of your images, any idea what the diagonal and horizontal lines are? It roughly looks like the trigger is firing when the character collider penetrates an amount equal to the height of that small rectangle. Maybe it's a coincidence.

    Are you finding this doesn't occur if the box collider has a scale of (1,1,1), and this is only happening when you've scaled the box?
     
  5. cacotigon

    cacotigon

    Joined:
    Jun 12, 2015
    Posts:
    9
    yeah 100% sure, I read the same documentation

    "Trigger events are only sent if one of the Colliders also has a Rigidbody attached."

    This does not appear to be true, since neither the Player (character controller) nor the platform (Cube mesh and box collider) has a rigidbody, so maybe the character controller has one underneath the covers?

    The box primitive is scaled at 1, 0.25, 1, and the box collider has been increased along the Y axis. I even made a simple unity project just to test this that I've attached to this reply - just play and drag the player down into the box collider and it'll trigger, no rigidbodies involved.
     

    Attached Files:

    Last edited: Oct 30, 2019
  6. dgoyette

    dgoyette

    Joined:
    Jul 1, 2016
    Posts:
    3,679
    Yeah, that doesn't make much sense to me, either why it's triggering the trigger at all, or why it requires significant penetration before doing so.

    I tried out your sample scene, and I get the same results. It probably requires around 0.2 units of penetration before the trigger fires. This seems to be the case even if I set the box's scale to (1,1,1), and set the collider dimensions to (1,1,1) as well.

    For comparison, I also added a simple Capsule with a Capsule Collider instead of the Player. With no rigidbody on it, it doesn't trigger OnTriggerEnter. And if I add a kinematic rigidbody to the capsule, it triggers OnTriggerEnter exactly when expected.

    So, this does seem like some strange behavior with the StandardAssets Character Controller. Maybe it assumes you'll place a rigidbody on the character? Not sure. You should consider reporting this as a bug. Also consider using a different character controller. I believe Standard Assets includes a Rigidbody character controller as well.
     
  7. cacotigon

    cacotigon

    Joined:
    Jun 12, 2015
    Posts:
    9
    Interesting.

    I also did some further tests - for example, manual raycasting from the bottom of the player capsule with an extremely small distance 0.005f and it detects perfectly. Seems like we're both in agreement, the Character Controller just doesn't interact correctly with Trigger-based Box Colliders. I'll give that "rb character controller" a shot.

    In the meantime, I guess I'll file this as a bug with Unity.
     
unityunity