Detecting a Transform/GameObject entirely comprised in a larger trigger


I need to know whether it is possible for a trigger to go off only when a smaller Transform or GameObject is totally enclosed in it, as opposed to whenever said Transform / GameObject merely breaches the “walls” of the trigger (which is what is detected with OnTriggerEnter(), if I’m right).

Thank you all for you help on that matter,

Ari ;o)

You can calculate the distance between the two pivots of the objects:

float threshold = 1;
float pivotDist = Vector3.Distance(objectA.transform.position,objectB.transform.position);
if(pivotDist < threshold)print("Object B is entirely enclosed");

Depending on how big the objects are you should optimize the threshold and this should work.

Eventually you can put this in a function that can return true or false. F/E:

bool isInside(Vector3 bigPos, Vector3 smallPos, float threshold)
    if(Vector3.Distance(bigPos,smallPos)< threshold)
    return true;
    else return false;

And then in your OnTrigger function you can use this function in an if statement.

What I can think of is that you can make “sensors” on your player. Let’s say that you make 4 of these “sensors”. Front, Back, Left, Right - to define the boundaries of the player’s body. These have to be very small sensors (use spheres of size 0.01f for example).

When any sensor enters the trigger (OnTriggerEnter), you increment an integer (counter). When the counter is 4, then it means that all 4 sensors are inside the trigger and therefore the ENTIRE player is inside.

Obviously, you have to couple this with the OnTriggerExit to decrement the counter.