OverlapSphere with Box Collider 2d

I’m trying the new 2D features of unity, and I create a sprite object, add a box collider 2D, and a simple script that uses OverlapSphere() to detect colliders. It returns 0 colliders, even when I attach this script to an object that has a collider attached to it:

// Use this for initialization
void Start () {
	Collider[] colliders = Physics.OverlapSphere(transform.position, 5);
	Debug.Log(colliders.Length + " colliders in range ");
}

Obviously, at least the object itself should appear in the results… Changing to regular box collider (3D) works fine.

Any ideas? Unity bug?

2 Answers

2

Along with collider2D, they also have the Physics2D library, you may be able to find the answer there. cheers mate :slight_smile:

added it per your request :)

Apparently there’s a Physics2D class, which handles physics in 2D modes. There’s a matching Physics2D.OverlapCircleAll() which is the OverlapSphere equivalent of 2D.

I guess the most important thing I learned is to read the release notes which state the new classes for using 2D.

perhaps you will just have to make 2 separate calls then? one for sphere and one for circle

That's what I'm looking for!

GetWorldCorner returns world coordinates. I'd guess your element was about 4.5 meters/units big. This is not returning pixels as far as I've found.