Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
avatar image
6
Question by ATMEthan · May 29, 2013 at 09:06 PM · boxcollidercollisiondetection

Test if BoxCollider is within another BoxCollider?

Hello Unity Community,

Besides writing my own collision detection or using the OnTriggerEnter/Stay/Exit or OnCollisionEnter/Stay/Exit is there another method to determine if a BoxCollider has a part of another BoxCollider inside it touching it?

-Ethan

Comment
Add comment
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

4 Replies

· Add your reply
  • Sort: 
avatar image
10
Best Answer

Answer by ATMEthan · May 29, 2013 at 09:12 PM

And the answer is yes! You can use the BoxCollider.bounds.intersect method.

something like this:

 if(topHeaderBoxCollider.bounds.Intersects(currentHeader.boxCollider.bounds))
Comment
Add comment · Show 4 · 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 ATMEthan · May 29, 2013 at 09:14 PM 0
Share

I didn't check if there any other methods. I just know that once I wrote this question I remember about the bounds variable in the BoxCollider and thought that should have something and it did.

avatar image rkstormer · May 20, 2014 at 07:20 PM 1
Share

The downside of this solution is that I believe it only works properly if both BoxColliders are axis-aligned. I still can't find a good general solution to check if two BoxColliders (or other type of colliders) intersect without needing to attach non-kinematic rigid bodies.

avatar image VWGoA · Apr 20, 2020 at 10:14 PM 0
Share

To solve the problem when Bounds are not mutually axis aligned, you could do something like this: Define 2 bounds each in the local space of 2 objects/transforms. They are each axis aligned in their local space, but not mutually axis aligned. For each corner point in bound1 (8 box points), transform the point, transform1. localToWorld.$$anonymous$$ultiplyPoint(), then transform2.worldToLocal.$$anonymous$$ultiplyPoint(). Test if the transformed point is within bound2 - bound2.Contains(xformedPoint). You'll also need to do the same with each point of bounds2, xform to the space of bound1, test if within the bound. If any point is within a bound, then break, no need to test the rest of the points.

avatar image Amchy · Feb 17, 2021 at 08:32 PM 0
Share

Thanks man so much it really helped me!!

avatar image
1

Answer by andreim44 · Mar 05, 2016 at 04:04 PM

I'm using the OnTriggerStay() method with the bounds.Contain() method to check this:

 void OnTriggerStay (Collider other)
     {           
         if (other.bounds.Contains(myCollider.bounds.min) 
              && other.bounds.Contains(myCollider.bounds.max))
         {
             // Inside the box collider
         }
     }
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 Ca-Spexter-Man · Mar 19, 2021 at 11:33 PM 0
Share

Worked for me. Thanks man!!! I never knew there were functions for checking this.

avatar image
0

Answer by UnPluks · Mar 16, 2016 at 05:43 PM

Hi! I know this topic is very old, but I would like to know what you guys think about this You can try to write your own function, using the vertices of you cube

 void CheckBoxIntersection(GameObject go){
 //Get the mesh you want to check
         Mesh mesh = go.GetComponent<MeshFilter>().mesh; 
         Vector3[] vertices = mesh.vertices;
         int i = 0;
         while (i < vertices.Length) {
             //Raycast from the middle to each vertex
               Ray ray = new Ray(go.transform.position, transform.TransformPoint(vertices[i]) 
               RaycastHit hit;
 
            if (Physics.Raycast(ray, out hit, 100)) 
            Debug.Log("Intersecting " + hit.collider.name);
         }
 }

Or if you want to go more precise, you could raycast three times per vertex, racasting from each vertex to the adjacent ones, or something like that, I haven't actually try this, I am just trying to brainstorm with you guys. Any thoughts about this would be nice

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 highboy_coupe · Apr 04, 2017 at 05:02 AM

In addition to the comment above by @UnkPluks, a slight amendment would be:

 for (int i = 0; i < vertices.Length; i++)
         {
             //Raycast from the middle to each vertex
             Ray ray = new Ray(go.transform.position, transform.TransformPoint(vertices[i]));
 
             RaycastHit hit;
 
             if (Physics.Raycast(ray, out hit, 100))
                 print("Intersecting " + hit.collider.name);
         }

otherwise you have an infinite loop going on because nothing is decreasing i, which causes a crash.

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

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

21 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

Related Questions

CharacterController with BoxCollider 1 Answer

object passing through walls and floors 1 Answer

2D Object Collision - How To? 2 Answers

Help with Circle Overlap Collision Detection 0 Answers

Box collider fell "inside" into another one (Collision) 3 Answers


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