- Home /
Detecting inside an area without a collider
Is it possible to detect an object inside a certain area (like a trigger collider) with the object you want to detect having the collider turned off? Sorry if this question sounds a bit weird. I'm very tired.
Answer by Bunny83 · Aug 26, 2017 at 01:21 PM
Sure, there are several ways. If it doesn't need to be precise or if the area is always a world space aligned box you can simply test against a "Bounds". A Bounds struct can be aquired from a renderer or a collider. However you can also specify one with arbitrary values. Just a center and a size value that specifies the box. You can use Bounds.Contains to check a worldspace position if it's inside the bounds. You can also check if two bounds overlap with Bounds.Intersects.
If you need more precision (for example a rotated box) you can do the check manually. You can either use a BoxCollider to specify the area, use it's center and size properties to specify a localspace Bounds value and just use InverseTransformPoint of the collider object's transform to convert the point you want to test into local space of the collider. Now you can simply use Contains:
public static bool IsInsideBoxCollider(BoxCollider aCol, Vector3 aPoint)
{
var b = new Bounds(aBox.center, aBox.size*0.5f);
var p = aBox.transform.InverseTransformPoint(aPoint);
return b.Contains(p);
}
Is there also a way to retrieve the object? $$anonymous$$aybe something like BlaObject = IsInsideBoxCollider.Gameobject?
Uhm, i'm not sure you understand how you should use this. You have to test a given position if it's inside the area / volume. So you have to manually test every object position. This works purely on the provided data.
The physics system is actually optimised in that regard. If you want to test many objects, using the physics system (rigidbody + collider) would be faster.
Just to make that clear. Say you have an array of 10 objects and you want to know which of those 10 objects is inside the area. You would need to iterate through the array and test them one by one. If the test is successful you know that the object you're currently testing is inside.
We could give you a much more specific answer when you ask a more specific question. What do you actually want to achieve? Why don't you want to use colliders? You know that you can put colliders onto a seperate layer so they don't interfer with other things? You can specify which layers can collider with with other layers.