- Home /
detect if a "Empty" is inside a box or trigger?
is there a way to detect if a Empty is inside a box?
I dont want to use collision detection because that would need the physic engine and I would need to use this on many gameobjects.
I want that this empty or the box can detect this so I can change the texture of this object that holds this Empty. there are more such boxes side by side and if I would use a collision detection or a trigger detection this would produce errors because if I would just touch the next cube with my object and would then go back it would change the texture to the next one and that is what I try to avoid.
Answer by robertbu · Jan 19, 2014 at 02:58 AM
There is a bunch of missing information for me to give you a detailed answer. One approach is to test the position of the empty game object against the bounds of true collider. I don't know how you are referencing the empty and the cubes, but something like:
if (collider.bounds.contains(other.position)) {
Debug.Log("Point inside collider bounds");
}
Note I believe this bounds is axis aligned, so this is not a perfect solution if there is any rotation in the cubes. An alternate solutions that is a bit more accurate: transform the position of the empty game object into local space (Transform.InverstTransformPoint()), then check it against the bounds of the mesh (Mesh.bounds).
hmm... almost. I think you gave me the right direction ^_^
btw the cubes I sayed are not necessary to be displayed or real there. they all axis-aligned bounding boxes (AABB). I will display the cubes by Gizmos.
I just need to know in which AABB the empty is and send to the empty some informations like "change texture to red" or something.
If you have an array of axis-aligned bounding boxes, just cycle through the array and use Bounds.contains() to check to see if the point is within the bounds.
like
var Emptys = new Array(); // I would later remove or add Objects with emptys
var aabb = Bounds (Vector3.zero, Vector3 (1, 2, 1));
foreach(var object : Vector3 in Emptys)
{
aabb.bounds.contains(object);
if (aabb.bounds.contains == true)
send infos
}
but how big would be the impact on the system when I have like 100-200 objects with that empty on it and like 40-80 Bounding Boxes? I would only need to check every 3 or 4 fixed update steps.
First off, don't use the 'Array' class for this. It is slow and untyped. Use the .NET List class. As for speed, you'll have to test to see. Even if you don't have the rest of your logic figure out, it is easy to create the 80 BB and 200 object and test on your target platform. If it is too slow, try spreading the whole check over multiple frames. Given what you are doing here, there is also the possibility of doing it in another thread (not coroutine). And there are other data structures that can help you.
This is getting a long ways from the original question. I suggest you open it as a new question with a detail explanation of what you want. Yes it is possible, but I don't understand enough of the particulars from your comment to indicate how you should accomplish your task. The List class has a number of methods for finding things in the list. Reference link:
http://msdn.microsoft.com/en-us/library/6sh2ey19(v=vs.110).aspx