# Differentiate an overlapping collision from just touching

So, I have a grid and you can build fixed size walls in that grid. I’m using simple onTriggerEnter/onTriggerExit to determine where I can/can’t build walls. Now the problem was, onTriggerEnter is also fired off when the objects just touched, so I could not build adjacent walls. I fixed that by setting the collider size to 99% of the mesh in all dimensions. The meshes could touch, but not overlap (as the grid is larger than the 1% difference).

Now, I’m trying to set up a way to merge my wall meshes to get a performance boost down the line. The simplest way would be to just merge everything that is touching… but because of the above mentioned 99% sized collider box, technically, nothing in the grid ever touches.

I thought about calculating distance and comparing to object size, but you can build in all 3 dimensions and you can modify the size of your construction piece in all 3 dimensions, so everything’s irregular and checking against every other thing in the scene (and I’m thinking of hundreds/thousands of pieces down the line) would be a pain.

So the problem in a sentence: how do I tell the difference from when two colliders are actually inside each other and from when they just share a few surface coordinates? That’s the minor issue though. The major issue is merging meshes that are close by each other. If not with collision, how else would I go about doing that? I’m not asking for code here, just give me ideas what to do, and I’ll try to work it out.

Edit: In that case –
The easiest way then, I can think to do this is, assuming both colliders are cubes only, and one entering the other is small in size, is to test the distance between the centers of the colliders. This is by far the easiest and faster.

If not cubes, then a more generic function is needed.
I don’t think Unity has an “OnInside” function exposed, surprisingly but it’s not difficult, just six possible cases to test:

``````//Psuedo Code:
bool IsInside(Box other)
{
return maxEdge.X <= other.maxEdge.X &&
maxEdge.Y <= other.maxEdge.Y &&
maxEdge.Z <= other.maxEdge.Z &&
minEdge.X >= other.minEdge.X &&
minEdge.Y >= other.minEdge.Y &&
minEdge.Z >= other.minEdge.Z;
}
``````

Should work for all axis aligned stuff (i.e. world coordinates)

OLD:
I think you need something like:

``````bool m_bSomethingIsTouching = false;
//check if it enters
//note, this script only works iff just one object will ever move into another

void OnTriggerEnter() {
m_bSomethingIsTouching = true;
}

void Update() {
if (m_bSomethingIsTouching) {
if (gameObject.collider.bounds.Contains(enteringObjPosition)) {
// Something is inside!
}
}

void OnTriggerExit() {
m_bSomethingIsTouching = false;
}
``````

as for merging meshes, i’ll let someone more qualified answer that. should probably break this into two separate questions.