## Unity: Is this point inside a box?

See if a point is inside a box by doing a reverse transform on the point into box local space.

bool PointInBox(Vector3 a_point, BoxCollider a_box) { // Transform the point into box local space a_point = a_box.transform.InverseTransformPoint(a_point) - a_box.center; // see if the point is inside the box local space Vector3 half = a_box.size * 0.5f; if (a_point.x < half.x && a_point.x > -half.x && a_point.y < half.y && a_point.y > -half.y && a_point.z < half.z && a_point.z > -half.z) { // Get a 0.0 to 1.0 range for each axis Vector3 current = a_point + half; m_last.x = current.x / a_box.size.x; m_last.y = current.y / a_box.size.y; m_last.z = current.z / a_box.size.z; m_lastPoint = a_point; // Set the last valid point inside the test area return true; } return false; }

Advertisements