I'm trying to detect if a point is within my box collider, with this function:
public bool PointInside(Vector3 point) {
return collider.bounds.Contains(point);
}
However, it always returns false, no matter what. The gameobject this function is run on has the collider as a trigger, and it is not rotated or scaled at all. I double checked the bounds of the collider are correct, which they are using this script https://discussions.unity.com/t/bounds-contains-is-not-working-at-all-as-expected/671128/5. The point passed into the function is in world-space, from a transform.position.
#Can't properly detect if a point is within a Box Collider's bounds
1 messages · Page 1 of 1 (latest)
This check the point in the local space of the bounds, not world space
You could use one of the overlap queries to find overlapping colliders.
https://docs.unity3d.com/6000.1/Documentation/ScriptReference/Physics.OverlapBox.html
Tried with both
return collider.bounds.Contains(transform.position - point);
and
return collider.bounds.Contains(transform.worldToLocalMatrix * point);
to no luck. I can't use overlap queries, since that could get extremely expensive as there are many AudioRooms being updated every few game Updates. there'd be over 100 audio rooms, so i want to try and stick to AABB style detection if i can
Then you'll need to inverse transform the world position point to the collider object space:
https://docs.unity3d.com/6000.1/Documentation/ScriptReference/Transform.InverseTransformPoint.html
And btw, an overlap query might not be too different in terms of performance(at least the non alloc one).
And if you're gonna go all out with the optimization, just having a collider in the scene gonna make impact on performance, so get rid of it as well.
good point, thanks.
I tried this
public bool PointInside(Vector3 point) {
bool inside = false;
foreach (Collider collider in Physics.OverlapSphere(point, 0.1f)) {
if (collider == this.collider) {
inside = true;
break;
}
}
Debug.Log(inside);
return inside;
}
Still not working. I'll try my own implementation for AABB detection.
Also, did try transform.InverseTransformPoint(), still no result
Then debug.
Print the relevant values or place a breakpoint and step through the code.
omfg im an idiot. What happened, was in my AudioRoomManager, when i added my code for skipping updates on intervals and checking all the rooms, i accidentally overwrote my code that would update the listenerPos, so the rooms were always checking against a point at 0,0,0, which of course would always be false. My bad, sorry for wasting your time. But I definitely did have to convert to local space for the collider. Works fine now.
You should use ClosestPoint for this check, not the Bounds