Test to see if a Vector3 (point) is within a boxcollider.

Is there a way to get the 3 rotated axis of the box collider to test if a point lies within it?

The quickest technique I know to obtain a boolean result for point within an OBB relys on axis, center, and the box's extent, However I cannot find a way to get the axis.

Also a late reply, but this question is high on the Google search results for OABB checks with BoxColliders, so worth mentioning that something like this would probably be faster than a raycast:

	bool PointInOABB (Vector3 point, BoxCollider box )
	{
		point = box.transform.InverseTransformPoint( point ) - box.center;
		
		float halfX = (box.size.x * 0.5f);
		float halfY = (box.size.y * 0.5f);
		float halfZ = (box.size.z * 0.5f);

		if( point.x < halfX && point.x > -halfX && 
		   point.y < halfY && point.y > -halfY && 
		   point.z < halfZ && point.z > -halfZ )
			return true;
		else
			return false;
	}

Works by transforming your point into the box’s local space and then checking against the box dimensions directly.

Maybe you could use Bounds.Contains on Collider.bounds.

This was three and a half years ago, but useful tip for others who come across this question: put the box in it’s own layer, cast a ray from the point your checking to the center of the box, and make sure your layermask is only that layer. If your raycast returns false, the point IS inside the box, if it returns true, your point IS NOT inside the box. Then if you still care what layer the box is in, return it to the layer it was in.

So, I know this is an ancient thread, but I found a neat way to do the same thing without all the branches, as min and max are often implemented without comparisons. It’s pretty much doing the same thing as above, just differently.

static bool IsInsideBounds(Vector3 worldPos, BoxCollider bc)
{
    Vector3 localPos = box.transform.InverseTransformPoint(worldPos);
    Vector3 delta = localPos - bc.center + bc.size * 0.5f;
    return Vector3.Max(Vector3.zero, delta)==Vector3.Min(delta, bc.size);
}

Nice method. Very useful.

But this kind of method definitively should be in a static util class.

And why this if / else statement ?

So as an instance method :

/// <summary>
/// Return true if the point is inside the given BoxCollider.
/// </summary>
/// <param name="p_Point"></param>
/// <param name="p_Box"></param>
/// <returns></returns>
public static bool IsInside(this Vector3 p_Point, BoxCollider p_Box)
{
    p_Point = p_Box.transform.InverseTransformPoint(p_Point) - p_Box.center;

    float l_HalfX = (p_Box.size.x * 0.5f);
    float l_HalfY = (p_Box.size.y * 0.5f);
    float l_HalfZ = (p_Box.size.z * 0.5f);

    return (p_Point.x < l_HalfX && p_Point.x > -l_HalfX &&
        p_Point.y < l_HalfY && p_Point.y > -l_HalfY &&
        p_Point.z < l_HalfZ && p_Point.z > -l_HalfZ);
}

Same with SphereCollider

/// <summary>
/// Return true if the point is inside the given SphereCollider.
/// </summary>
/// <param name="p_Point"></param>
/// <param name="p_Box"></param>
/// <returns></returns>
public static bool IsInside(this Vector3 p_Point, SphereCollider p_Sphere)
{
    p_Point = p_Sphere.transform.InverseTransformPoint(p_Point) - p_Sphere.center;
    return p_Point.sqrMagnitude <= p_Sphere.radius * p_Sphere.radius;
}

And maybe when all type all colliders are supported, do a method like that :

/// <summary>
/// Is the Point inside the given Collider ?
/// Must be a convex collider.
/// </summary>
/// <param name="p_Point"></param>
/// <param name="p_Collider"></param>
/// <returns></returns>
public static bool IsInside(this Vector3 p_Point, Collider p_Collider)
{
    SphereCollider l_SphereCollider = p_Collider as SphereCollider;
    if (l_SphereCollider != null)
    {
        return p_Point.IsInside(l_SphereCollider);
    }

    BoxCollider l_BoxCollider = p_Collider as BoxCollider;
    if (l_BoxCollider != null)
    {
        return p_Point.IsInside(l_BoxCollider);
    }

    //etc...
 }

Does anybody know how to do this when the box collider isn’t known.

In my example;
I have a unknown number of box colliders in the game and I want to check if a vector3 contains any box collider?

All the examples I see are only good if you already know the box collider you’re checking…