Search Unity

Is it possible to override MeshCollider bounds? Make flat collider work with raycasting

Discussion in 'Physics' started by swedishfisk, Apr 17, 2021.

  1. swedishfisk

    swedishfisk

    Joined:
    Oct 14, 2016
    Posts:
    57
    I have generated a single triangle mesh that is flat (all points lie on y = 0). I set this as collision mesh using MeshCollider.SharedMesh.

    Now say I want to inflate it to make it thicker, for instance to make RayCasting from x, z plane possible (if collider bounds height is 0f no RayCasting, SphereCasting or OverlapSphere will detect it, i guess sense it doesn't exist at all in that dimension).

    I essentially want to find intersections with this flat triangle using line segments that also lie in the x, z plane.

    Just settings MeshColliders.SharedMesh bounds will work, and I can log to see that the change is sticking. But setting MeshCollider.bounds doesn't stick. Unity seem to cache the original values and they get reset to match the mesh shape.

    This makes sense because the AABB of colliders do get automatically resized when rotating the transform so the collider is always inside the AABB, so I assume the colliders bounds just gets overwritten by Unity on each update.

    But is there any way to inflate the bounds in a clean way? I just want to make Bounds.Intersect, Physics.OverLap sphere etc. work because they don't (unless I enable convex mesh collider).

    Edit: I've made some progress detecting hits by messing with the normals, I would much rather have a clean way of adding some padding to the collider without messing with the vertices though.
     
    Last edited: Apr 17, 2021