Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Mesh Collider: Unexpected isTrigger Behavior

Discussion in 'Editor & General Support' started by NAUIVRLab, Feb 18, 2019.

  1. NAUIVRLab

    NAUIVRLab

    Joined:
    Feb 15, 2019
    Posts:
    3
    I have a Unity scene with two Colliders: A cube with a box collider that moves back and forth through the air, and a tube with a mesh collider (the tube and mesh collider are created and attached during run-time) placed in the way of the cube such that the two will intersect multiple times while the scene is in play.

    Both objects have a Rigidbody component, with useGravity = false, and isKinematic = true.

    The tube mesh collider has isConvex = true, and isTrigger = true. The box collider has isTrigger = false.

    A script attached to the cube has a definition for OnTriggerEnter:
    private void OnTriggerEnter(Collision other)
    {
    Debug.Log("COLLISION DETECTED");
    }

    Expected Behavior: I press play to start the scene, and the console should output a message for each time the cube intersects the tube mesh.

    Actual Behavior: No output is logged when the cube intersects the tube mesh, indicating the collision was not detected. HOWEVER, if, during play mode, I toggle isTrigger to false and then to true again, the tube mesh will correctly register collisions to the console.

    It's clear that some part of the game state is being updated when I toggle the isTrigger value in the inspector and that the mesh collider detects collisions correctly after that point. However, I need the mesh to detect collisions without a manual toggle during play mode. It should work when the program starts, as isTrigger is already set to true (the inspector confirms this with the check mark when starting the game).

    This seems like a bug to me. I could potentially get around this problem by just manually calling whatever event/function occurs when the inspector values are changed, but that's still sloppy.

    Any ideas?
     
  2. NAUIVRLab

    NAUIVRLab

    Joined:
    Feb 15, 2019
    Posts:
    3
    Update: It looks like the mesh collider has no rigidbody attached, even though there is one attached to the parent object, until isTrigger is changed in the inspector. Before isTrigger is toggled, meshCollider.attachedRigidbody() returns Null, but after isTrigger is toggled, it returns the rigidbody object attached to the parent object.

    Is it possible to assign a rigidbody to the collider manually during run-time?