Hi guys, I’m trying to create a “constrain area” to detect if a square object left or enter inside another square object.

I would like to detect when the square B is completely inside the square A, and if I move out the square B, when he leave the square A, I receive a “warn”.

I’m combining OnTriggerEnter, OnTriggerExit, OnTriggerStay, and using Intersects to try to make it work

Debug.Log(B.bounds.Intersects(A.bounds));

But now the problem is:

when the square B enter inside square A, It returns true (10% inside), also (100% inside)

when the square B leave the square A, (10% outside) It returns true, but
when it is (100%) returns false.

Bounds is an AABB - Axis Aligned Bounding Box. It’s the minimum box aligned to the axes XYZ that fully contains the object. Since Bounds is axis aligned, an object that isn’t aligned to the axes (a rotated cube, for instance) may occupy a bigger box. Unity does a first quick analysis by checking intersections between the bounds of the object and the collider (or trigger): if there’s any intersection between the bounds, a more complete test is performed to check the actual object shape.
OnTriggerEnter occurs when the object first intersects the trigger, and OnTriggerExit warns when the object fully exits the trigger. Knowing whether the object is fully inside the trigger requires more complex tests - in the case of a box, you should check whether all the box’s vertices are inside the trigger. This answer shows a method that could be used for checking each vertex - but you must calculate the world coordinates of each vertex of both, the object and the trigger.
If you’re using the standard Unity cube for both, all the vertices are at 0.5 units in each axis:

Front face: (-0.5,0.5,-0.5), (0.5,0.5,-0.5), (0.5,-0.5,-0.5), (-0.5,-0.5,-0.5)

Back face: (-0.5,0.5,0.5), (0.5,0.5,0.5), (0.5,-0.5,0.5), (-0.5,-0.5,0.5)

The world coordinates can be calculated by transform.TransformPoint(vertex) - it takes into account the transform’s scale, rotation and position, thus the resulting point will be the actual vertex position.

Hi! This is my solution.
To fully understand it you should understand the basic idea of coordinate spaces. Local, global, inverse and all that. If You don’t understand… Well, it takes maybe 30 minutes if someone explains it to you and maybe a little longer if you do the googling yourself. I recommend learning it.

using System.Linq;
using UnityEngine;
public class SenseCompleteIntrusion : MonoBehaviour
{
/// <summary>Mesh filter of the object that is moving in and out of this gameobjects bounds.</summary>
public MeshFilter OtherMeshFilter;
//visualize colors for debug.
public Color IntrudedColor = Color.red;
public Color InitialColor = Color.gray;
/// <summary>Mesh filter of the gameobject this script is attached to.</summary>
private MeshFilter _myMeshFilter;
/// <summary>Other meshfilter mesh bounding box corners in world space. These corners are not aligned to global axes.</summary>
private Vector3[] _otherCornersInWorldSpace = new Vector3[8];
private void Start()
{
_myMeshFilter = GetComponent<MeshFilter>();
}
//I'm just polling. For simplicity.
private void Update()
{
//We get other meshfilter bounds corners in this gameobjects local space.
var otherCornersInMySpace = OtherCornersInMySpace();
//We now check if other corners in this gameobject's space are inside this gameobjects
//bounds (also given in this gameobject's space) The check is done in common space. Obviously:)
GetComponent<Renderer>().material.color = AreInMyBounds(otherCornersInMySpace) ? IntrudedColor : InitialColor;
}
private Vector3[] OtherCornersInMySpace()
{
//Meshfilter.sharedMesh.bounds gives us bounds in owning gameobjects local space.
//First we take other corners and transform them into world space (transformed in GetCornersInWorldSpace).
GetCornersInWorldSpace(OtherMeshFilter.sharedMesh.bounds, OtherMeshFilter.transform, ref _otherCornersInWorldSpace);
//Then we tansform them into this gameobjects local space.
Vector3[] otherCornersInMySpace = new Vector3[8];
for (int i = 0; i < 8; i++)
{
otherCornersInMySpace <em>= transform.InverseTransformPoint(_otherCornersInWorldSpace*);*</em>

* }*

* return otherCornersInMySpace;* * }*

* private bool AreInMyBounds(Vector3[] otherCornersInMySpace)* * {* * var myBounds = myMeshFilter.sharedMesh.bounds;
_//google “linq all” if this line confuses you* * return otherCornersInMySpace.All(corner => myBounds.Contains(corner));* * }*

@salsa There isn’t much to answer without seeing the code, but there is an old algo for this. Let’s see, you just have to check that left edge of square B is to the right of left edge of square A and right edge of B is to the left of right of A. Similarly for the top and bottom edges, top edge of B should be below top edge of A and bottom of B should be above bottom of A. If all four conditions hold, then and only then B is inside A. There are many ways to check intersection but to check exactly inside, this is one of the algos. I really don’t think OnTriggerStay could differentiate between completely inside and just intersecting. And to check completely outside , just reverse the conditions. Oh!! instead of putting if statements twice you can use a bool, it’s all up to you. This is all I can say at the moment.

Hi, I’m new to Unity and I’m developing an augmented reality application and I have a problem similar to this one.

My situation is as follows: I have two objects in scene, a neighborhood model and a cube. The model will be the object that I will enlarge and the cube will be like the acrylic box where the model is. The cube represents the viewing area and will be transparent. The model has an animation in which it moves and scales, and the cube remains fixed.

I need something to assign to the cube so that when expanding the model, parts outside the cube have 50% colorless transparency (all gray) and the part of the model inside the cube (at the intersection) remains solid and with the colors and textures.

The idea is to present the model on an initial scale that is inside the cube (acrylic box), so that the model’s animation zooms in at certain points; the entire model will be much larger than the cube. The outside of the cube is transparent and colorless and the inside remains colored to make it more prominent.