Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
1
Question by salsa · Jun 26, 2017 at 04:25 AM · collisionintersect

How to detect if object is completely inside other object?

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.

Any help is appreciated ;)

Thanks :)

Comment
Add comment · Show 4
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image aldonaletto · Jun 26, 2017 at 12:23 PM 0
Share

Squares (2D figures with equal sides) or Boxes (3D figures with arbitrary proportions)?

avatar image Owen-Reynolds · Jun 26, 2017 at 06:19 PM 1
Share

Search "unity completely inside." One of the top results has an older answer by adoltanto (who commented on and answered your Q) which should work. Another one has my answer (which is about the same.)

avatar image salsa Owen-Reynolds · Jun 26, 2017 at 09:36 PM 0
Share

Didn't found it :(

avatar image aldonaletto · Jun 26, 2017 at 10:54 PM 0
Share

An old answer of $$anonymous$$e had a different and simpler approach: it used thin triggers covering the main trigger faces (thanks to @Owen-Reynolds for remembering this old answer)

5 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by aldonaletto · Jun 26, 2017 at 01:38 PM

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
1

Answer by akaivo · Jul 20, 2018 at 06:12 PM

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[i] = transform.InverseTransformPoint(_otherCornersInWorldSpace[i]);
         }
 
         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));
     }
     
     private static void GetCornersInWorldSpace(Bounds bounds, Transform trans, ref Vector3[] positions)
     {
         // Transform bounds local points to world space.
         positions[0] = trans.TransformPoint(bounds.min.x, bounds.min.y, bounds.min.z);
         positions[1] = trans.TransformPoint(bounds.min.x, bounds.min.y, bounds.max.z);
         positions[2] = trans.TransformPoint(bounds.min.x, bounds.max.y, bounds.min.z);
         positions[3] = trans.TransformPoint(bounds.min.x, bounds.max.y, bounds.max.z);
         positions[4] = trans.TransformPoint(bounds.max.x, bounds.min.y, bounds.min.z);
         positions[5] = trans.TransformPoint(bounds.max.x, bounds.min.y, bounds.max.z);
         positions[6] = trans.TransformPoint(bounds.max.x, bounds.max.y, bounds.min.z);
         positions[7] = trans.TransformPoint(bounds.max.x, bounds.max.y, bounds.max.z);
     }
 
 }
 

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
-2

Answer by Linkthehylian04 · Jun 26, 2017 at 03:02 PM

 public gameObject cube;
 public gameObject otherCube;
 
 void Update()
 {
      if (cube.transform.position == otherCube.transform.position)
      {
           //Whatever you want to happen
      }
 }
Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image W4rf4c3 · Jan 15, 2018 at 05:39 PM 0
Share

This will only work if the outside object is EXACTLY at the same position of the inner object. What if the inner object is a rectangle and the outside object is all the way inside but aligned left or right. This code won't trigger anything.

avatar image
0

Answer by Manpreet_96 · Jun 26, 2017 at 06:18 AM

@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. :D

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

Answer by jsatlher · Nov 28, 2019 at 03:59 PM

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.

Can someone help me?

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Owen-Reynolds · Nov 29, 2019 at 08:48 PM 0
Share

This is a question, not an answer. The system works best when each is in its place.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

106 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to check if gameObject is interact with multiple colliders 1 Answer

Make Object Change to Opposite direction on collision 0 Answers

Check if tag is the same as the previous one 1 Answer

CS1525 Unexpected symbol '=', HELP!!! 2 Answers

I have two different objects I would like to check when A and B gameObjects are collided ? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges