- Home /
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
}
}
}
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))
{
// [ ... ]
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.
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?
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
}
*/
}
}
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
Follow this Question
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