I have this gameobject with a collider component attached how do i test if the collider is fully covered by another and not just touching, i dont think it would be efficient to create other colliders objects as a child of the main because there will be lots of these objects. Is there a command i can use for scripting to detect this or something?
2D or 3D
3D
There’s no built-in way AFAIK, but making your own solution might not be very difficult if you’re just talking about sphere and box colliders. Just iterate through some set or subset of points (the eight corner points of the box collider, or six iterations of “closest point to target” using the six directions on a sphere), then check all of them for “is inside of collider bounds”. If all of them are, then you can be relatively certain the whole object is. With mesh colliders, it would be far more complicated and involve spaced sampling of vertices and blah blah blah- probably not worth it. You’d have to make do with using a simpler approximation using a box or sphere collider instead.
Yeah i knew about that but since i’m creating a lot of these objects there would be thousands of box colliders it would be inefficient and cause lag eventually. So i wanted to know if there is a script command that can detect this but apparently there’s not. There must be another way other then box colliders on the corners… Right?
As Lysander said, you wouldn’t need box colliders on the the corners. If you know at least one specific point of the boxes and the size of the boxes you can programatically check each point.
If the boxes are always aligned, not twisted relative to each other it becomes easier still:
To check if box A covers box B - Find max and min x, y, z values for both boxes. Then if Bmax X > Amax X, A does not cover B. If BminX < AminX then A does not cover B. Repeat the same tests for y and z values. 6 checks. If it “passes” all the checks its covered. Should be very quick code.
You misunderstand me- you don’t need to create more colliders, you just need to check if the points (vector3 positions) associated with one collider are inside of the larger collider. Each collider (if enabled) has a member called “bounds” which is a “bounding box”, or basically the geometric shape that’s generated for use by the collider. Bounds have a method you can call, “Contains()”, which will check a Vector3 to see if it’s inside of its bounded area.
So, how you would go about getting the six or so points (for collider1) you want to loop through depends on the type of collider. You could make a function that accepts two colliders as arguments and then uses the simpler one (cube for instance) for grabbing the “points” that you want to check, and then running them through the collider.bounds.Contains(point) of the more complicated collider (sphere or mesh or w/e).
Anyways, for a box collider, you just need to grab the transform.position (call it the originPoint) and then add the box collider’s “size” to the x axis, y axis, and z axis in every combination to get all of the points that make up the corners I think, then feed those points into the Contains of the second collider in a loop. If any fail, return false, and if the loop completes and moves on, return true. Something like that.
Get it? Hope this was helpful. ![]()
EDIT: Didn’t even consider the rotation of that box collider example- you can still use that method but instead of adding anything to the transform position you could calculate a series of new Vector3s from the “size” alone-
“0, 0, 0”
“size, 0, 0”
“size, size, 0”
“size, 0, size”
“size, size, size”
“0, size”, 0"
“0, 0, size”
“0, size, size”
- then run them all through transform.TransformPoint(vector) to get the real positions in world space. THOSE are the numbers you’d feed into the 2nd collider’s “contains” method.
Is there a way i can check if there’s a certain percentage of the object inside another, for example if 1/3 of a cube was inside another.
i don’t think there’s a function from unity so you have to do math yourself ![]()
As I said, there’s no function to even tell if the entirety of one collider is inside of another. That being the case, there’s zero chance that there’s a function that can do such a thing with even greater flexibility. If something could tell a third of it was inside, then it would be able to tell if all of it was inside. Get it?