Axis Aligned Bounding Box Vs Object Orientated Bounding Box

Hi guys,

I'm trying to get the closest point on the bounds of one object to another game object. This is what I am currently trying out:

public class object1 : MonoBehaviour
{
    void OnDrawGizmos()
    {
        Collider[] colliders = Physics.OverlapSphere(transform.position, radius);

        foreach (Collider hit in colliders) 
        {
            if( hit.CompareTag("Block") )
            {
                Gizmos.color = Color.red;
                Gizmos.DrawSphere( hit.ClosestPointOnBounds(transform.position), 0.1f );
            }
        }
    }
}

All this gives me is the AABB (Axis Aligned Bounding Box) of the hit object. Unity Uses the OOBB (Object Aligned Bound Box) for it's rigidbody collisions, but using `hit.rigidbody.ClosestPointOnBounds` only gives the AABB again.

Is there a way to get the closest point on the OOBB that isn't computationally expensive?

Or at the very least accessing the OOBB which is used by the rigidbody?

I think you are misunderstanding the terms box collider and bounding box.

A (axis aligned) bounding box is used for quick culling of objects. This is the bounds you're trying to get the closest point from with that call, not the actual box collider data.

you could do like this though:

// ...

    Collider[] colliders = Physics.OverlapSphere(transform.position, radius);

    foreach (Collider hit in colliders) 
    {
        if( hit.CompareTag("Block") )
        {
            var dir = hit.transform.position - transform.position;
            var ray = new Ray(transform.position, dir.normalized);
            RaycastHit rayhit;
            if (hit.Raycast(ray, out rayhit, dir.magnitude)) {
                // rayhit.point now contains the closest point
                Gizmos.color = Color.red;
                Gizmos.DrawSphere( rayhit.point, 0.1f );
            }
        }
    }    

// ...

I am not sure how the Unity engine performs collision detection but you could try using the AABB reported by Mesh.bounds.