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 /
avatar image
0
Question by MadManAdam · Aug 02, 2018 at 06:25 AM · scripting problemscripting beginnerboundsboxcollider2dintersect

BoxCollider2d.bounds.Intersects Not Working

Hi all, I am trying desperately to get BoxCollider2d.bounds.Intersects working and it is not happening at all. I am using this is a basic example: https://docs.unity3d.com/ScriptReference/Bounds.Intersects.html

I think the script linked is outdated but I changed the parts to GetComponent and it still doesn't work, intersection isn't detected. I should also note that I have tried attaching 2d rigid body to my main character, marked as trigger, unmarked as trigger and it makes zero difference. Please help me. Thank you for reading and for your time. The script is attached to an empty game object. I am also aware that GameObject.Find is bad, I won't do that a lot. :)

My code is this: using System.Collections; using System.Collections.Generic; using UnityEngine;

 public class checking_bounds : MonoBehaviour {
     
     public Collider2D object_that_can_be_entered_collider;
     public Collider2D maincharactercollider;
     public GameObject maincharacter;
     public GameObject object_that_can_be_entered;
 
     // Use this for initialization
     void Start () {
         maincharacter = GameObject.Find("MainCharacter");
 
         object_that_can_be_entered = GameObject.FindGameObjectWithTag("CanEnterObject");
         if (maincharacter != null)
             maincharactercollider = maincharacter.GetComponent<BoxCollider2D>();
         Debug.Log(maincharacter.GetComponent<BoxCollider2D>().bounds); // this works 
 
         if (object_that_can_be_entered != null)
             object_that_can_be_entered_collider = object_that_can_be_entered.GetComponent<BoxCollider2D>();
         Debug.Log(object_that_can_be_entered_collider.GetComponent<BoxCollider2D>().bounds); // this works 
             
     }
     
     // Update is called once per frame
     void Update () {
         
         //Debug.Log(hero.GetComponent<BoxCollider2D>().bounds);
 
         if (maincharactercollider.GetComponent<BoxCollider2D>().bounds.Intersects(object_that_can_be_entered_collider.GetComponent<BoxCollider2D>().bounds))
         {
             Debug.Log("Bounds intersecting"); // this doesn't work
         }
 
     }
 }
 
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

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by Bunny83 · Aug 02, 2018 at 07:12 AM

Keep in mind that the Bounds struct is a pure 3d construct. Also keep in mind that it does not represent the actual collider but it's AABB (axis aligned bounding box). Your two bounds might not be at the same z distance or might have a size of 0 along z.

 void Update()
 {
     Bounds charBounds = maincharactercollider.bounds;
     charBounds.extents += Vector3.forward * Mathf.Infinity; // scale the bounds to infinity on z
 
     Bounds objBounds = object_that_can_be_entered_collider.bounds;
     objBounds .extents += Vector3.forward * Mathf.Infinity;
 
     if(charBound.Intersects(objBounds))
     {
         // [ ... ]
Comment
Add comment · Show 2 · 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 MadManAdam · Aug 02, 2018 at 01:25 PM 0
Share

Thank you so much for replying!! This gets me closer. If I change both game objects to have the same z distance with my original code it works. If I use your code and I put my one game object back to z = 1 (ins$$anonymous$$d of 0, same as other game object) it does not work. I need the game objects to be at different z depths because I'm using On$$anonymous$$ouseDown. Is there anyway to work around this? Thank you again so much I appreciate it, a lot!! At the end of the day I need to be able to figure out if two game objects overlap.

avatar image Bunny83 MadManAdam · Aug 02, 2018 at 05:56 PM 0
Share

That's not really possible. The "Intersects" method just does this:

 public bool Intersects(Bounds bounds)
 {
     return $$anonymous$$.x <= bounds.max.x && max.x >= bounds.$$anonymous$$.x &&
            $$anonymous$$.y <= bounds.max.y && max.y >= bounds.$$anonymous$$.y &&
            $$anonymous$$.z <= bounds.max.z && max.z >= bounds.$$anonymous$$.z;
 }

$$anonymous$$ and max are properties which just do this:

 public Vector3 $$anonymous$$
 {
     get { return center - extents; }
 }
 public Vector3 max
 {
     get { return center + extents; }
 }

Since extents.z is +inf inside intersect the z check will always be true. $$anonymous$$.z is always "-inf" and max.z is always "+inf". So -inf is always smaller than +inf.


Do you use an orthographic camera that is looking straight at the x-y plane? If not you may have some paralax error so the object actually don't overlap, even it looks like they do.


edit I just checked what the bounds of a 2d box collider looks like:

So as you can see the center is just the objects world position (since the collider offset is "0, 0" ). The extent is 0 on z as 2d object do not have any size in the z direction. Setting the extend to +inf should fix the problem. Are you sure you really did the same i've posted?

avatar image
0

Answer by MadManAdam · Aug 03, 2018 at 05:16 AM

@Bunny83 - Yep, orthographic looking straight on at the x-y plane. I'm prototyping a top down game. I tried your code again I am getting the same result as before. Thanks for sticking with me on this. I think I'll be able to work around this by setting a boolean to fire off other code I need based on if an intersection as happening or not. It would have taken me a lot longer to figure out the z-axis on my own because I wrongly assumed a BoxCollider2D disregards the z-axis, and it looks like it doesn't.

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class checking_bounds : MonoBehaviour {
     
     public Collider2D object_that_can_be_entered_collider;
     public Collider2D maincharactercollider;
     public GameObject maincharacter;
     public GameObject object_that_can_be_entered;
 
     // Use this for initialization
     void Start () {
         maincharacter = GameObject.Find("MainCharacter");
 
         object_that_can_be_entered = GameObject.FindGameObjectWithTag("CanEnterObject");
         if (maincharacter != null)
             maincharactercollider = maincharacter.GetComponent<BoxCollider2D>();
         Debug.Log(maincharacter.GetComponent<BoxCollider2D>().bounds); // this works 
 
         if (object_that_can_be_entered != null)
             object_that_can_be_entered_collider = object_that_can_be_entered.GetComponent<BoxCollider2D>();
         Debug.Log(object_that_can_be_entered_collider.GetComponent<BoxCollider2D>().bounds); // this works 
             
     }
     
     // Update is called once per frame
     void Update () {
         
         Bounds charBounds = maincharactercollider.bounds;
         charBounds.extents += Vector3.forward * Mathf.Infinity; // scale the bounds to infinity on z
 
         Bounds objBounds = object_that_can_be_entered_collider.bounds;
         objBounds.extents += Vector3.forward * Mathf.Infinity;
 
         if (charBounds.Intersects(objBounds))
         {
             Debug.Log("Bounds intersecting"); // doesn't trigger
         }
          /*
         if (maincharactercollider.GetComponent<BoxCollider2D>().bounds.Intersects(object_that_can_be_entered_collider.GetComponent<BoxCollider2D>().bounds))
         {
             Debug.Log("Bounds intersecting"); // this now works if both z-axis is set to zero with my original code
         }
         */
     }
 }
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 MadManAdam · Aug 03, 2018 at 05:20 AM 0
Share

Could the z extent be changed so the game objects match at runtime? The x-y axis would still intersect so it should still work... yes, no?

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

159 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 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

What is the best way to detect collision?? Raycast2D, OnTriggerEnter2D, OnCollisionEnter2D 1 Answer

How to use GetComponent to modify variables of multiple gameobjects with the same script 2 Answers

Quiz Game : Valid Question keep decreasing each time the game start. 2 Answers

Why does my character pass through walls when he respawns and I lose control of him? 2 Answers

help with 3d scroller skateboard chase script 0 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