There are a few ways to do this.
You can get the Bounds of the collider, and use Bampf’s answer here to extract the points of the bounds (that answer is tailored to 3D but the idea is the same).
Or you can use TransformPoint with the collider’s center value to get a world position, and use its size value to get x and y positions based on half the width and height from that world version of the center point.
EDIT:
Here’s an example
BoxCollider2D collider = (BoxCollider2D) gameObject.collider2D;
Vector2 size = collider.size;
Vector3 centerPoint = new Vector3( collider.center.x, collider.center.y, 0f);
Vector3 worldPos = transform.TransformPoint ( collider.center );
float top = worldPos.y + (size.y / 2f);
float btm = worldPos.y - (size.y / 2f);
float left = worldPos.x - (size.x / 2f);
float right = worldPos.x + (size.x /2f);
Vector3 topLeft = new Vector3( left, top, worldPos.z);
Vector3 topRight = new Vector3( right, top, worldPos.z);
Vector3 btmLeft = new Vector3( left, btm, worldPos.z);
Vector3 btmRight = new Vector3( right, btm, worldPos.z);
This assumes the script is on the object with the collider (for fetching it), but what you do with the values and where you store them would be up to you.
EDIT 2: Rect example
If your collider is not going to change shape, only position, I’d say you should store info in a rect value that you simply change the center to match the world center of the collider. Then you can use xMin
/ xMax
and yMin
/ yMax
to quickly fetch the relative x and y positions of the corners, as follows:
BoxCollider2D collider = (BoxCollider2D) gameObject.collider2D;
Vector2 size = collider.size;
Vector3 centerPoint = new Vector3( collider.center.x, collider.center.y, 0f);
Vector3 worldPos = transform.TransformPoint ( collider.center );
Rect rect = new Rect(0f, 0f, size.x, size.y);
rect.center = new Vector2(worldPos.x, worldPos.y);
Vector3 topLeft = new Vector3( rect.xMin, rect.yMax, worldPos.z);
Vector3 topRight = new Vector3( rect.xMax, rect.yMax, worldPos.z);
Vector3 btmLeft = new Vector3( rect.xMin, rect.yMin, worldPos.z);
Vector3 btmRight = new Vector3( rect.xMax, rect.yMin, worldPos.z);
Necro Edit 3:
In response to a recent comment, I’d like to add a third alternative that is really like the first one but more flexible to z rotation. The main difference is that instead of immediately getting the world point of the collider and simply adding the x / y size (not accounting for rotation), you fetch the world point of each of the local points individually in local space (which does account for rotation).
BoxCollider2D collider = (BoxCollider2D) this.gameObject.GetComponent<Collider2D>();
float top = collider.offset.y + (collider.size.y / 2f);
float btm = collider.offset.y - (collider.size.y / 2f);
float left = collider.offset.x - (collider.size.x / 2f);
float right = collider.offset.x + (collider.size.x /2f);
Vector3 topLeft = transform.TransformPoint (new Vector3( left, top, 0f));
Vector3 topRight = transform.TransformPoint (new Vector3( right, top, 0f));
Vector3 btmLeft = transform.TransformPoint (new Vector3( left, btm, 0f));
Vector3 btmRight = transform.TransformPoint (new Vector3( right, btm, 0f));
For performance and efficiency, I advise caching these values when you can instead of always calculating them, including caching ‘transform’ and gameObject
if you are going to be calling this often.