- Home /
How do I find whether a point exists within the volume of a (trigger) collider?
I have a point in 3D space, represented in a Vector3 as you would expect. This may be the position of a game object's transform or a Vector3 completely unrelated to any existing transforms.
I also have a set of colliders set as triggers. For the purposes of this question, assume I've narrowed things down to a single collider I wish to consider.
So we have one point and one collider. I would like to determine whether the point lies within the collider's volume.
Bounds.Contains() is nearly what I want, but it's limited to an AABB that surrounds the collider. A large capsule collider at a significant angle would return true in cases where it should be false. Even if the AABB is in local space rather than world space, the curvature of the contained collider will still cause inaccuracy. Mesh colliders present a further challenge. Instead I want an analog at the collider level.
Check if position is inside a collider from an earlier Unity Answers question is also not quite what I'm looking for. It assumes use of colliders and rigidbodies, and my (limited) testing has shown that to be too slow for my needs.
Test if point is in Collider/Trigger is linked to from the previous answer, but also doesn't seem to cover my needs. The colliders could be quite large and the presented solution only works if the raycast passes through a face of the collider.
The physics system deals with volumes, and tests to see if they intersect. I find it odd that there doesn't seem to be an exposed way to test a singular point, as it seems to be a much less complex task.
Is there an existing library function to determine whether a point lies within a collider, or do I need to write my own? Writing my own would entail special casing for each class of collider and likely a large amount of redundant but slower code compared to what's already in the physics library.