How to get coordinates, width, and height from BoxCollider2D?

I’ve been trying to figure this out by Googleing for about 4 hours and nothing seems to work. I’m about to quit Unity already and just go back to using LibGDX and LWJGL. It’s killing me :confused: I’ve never had so much trouble just trying to get coordinates, and sizes from a video game object before honestly.

EDIT:

Converting pixels to units in orthographic projection mode requires two things, Camera.orthographicSize which is half of the vertical size of the viewing volume and Screen.height which is the current height of the screen window in pixels.

The number of units per pixel on the screen (or unit pixel ratio) can be given as

float unitsPerPixel = 2 * mainCamera.orthographicSize  / Screen.height;

With this ratio converting pixel values to unit values and vice versa is a trivial task. Multiply the pixel values with or divide the unit values by this ratio to get the values in the other unit.

NOTE that this value will have to be recalculated each time the screen resolution changes, that is at the start of the game, and after the resolution having been changed by the user.

Example: For a Full HD display/window height ( Screen.height = 1080 ) and a main camera with mainCamera.orthographicSize = 54, unitsPerPixel will evaluate to 2 * 54 / 1080 = 108 / 1080 = 0.1f. Thus 50 pixels on the screen equal 50 * 0.1 = 5 units in Unity and 15 Unity units equal 15 / 0.1 = 150 pixels on the screen for that specific resolution and specifc camera size.


Original answer:

The coordinates (or location) of the center of the BoxCollider2D is simply its (this code won’t compile) transform.position.xy + boxCollider2d.offset ( boxCollider2d.offset is boxCollider2d.center in versions older than 5.3.3). The offset is usually a null vector unless you change it manually, in which case the center of the BoxCollider2D is simply transform.position.xy which will be written in actual code as new Vector2(transform.position.x, transform.position.y)

The width and height of the BoxCollider2D are boxCollider2d.size.x and boxCollider2d.size.y respectively.

Always consult the docs whenever you need to look for something trivial like this. Honestly, if you weren’t able to look for how to obtain such trivial data even after hours of googling, you should learn how to google efficiently. Just removing the “How to” and the ? from your question and putting into the search box yields this answer as the second result (after this question of course) and the Docs for BoxCollider2D.size as the 4th result.

If you want the coordinates (accounting for rotation) of each of the corners of the BoxCollider2D, here’s how you’d do it:

    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));

This code has been taken straight from the second result (answer that I linked above).