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 :/ I've never had so much trouble just trying to get coordinates, and sizes from a video game object before honestly.
Answer by EvilTak · Mar 24, 2016 at 05:59 AM
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).
When I use collider.size.x it says the width of my object is 14, which is wrong. The width of the object is around 120 pixels on the screen. Is this because I am using the box collider on 3D text in my 2D game? What am I doing wrong?
Ah, then you should mention that you want the pixel coordinates and sizes. Unity uses metric units, that is one unit in Unity is 1 metre. This allows measurements and distances to be uniform no matter what screen resolution and aspect ratio the game is running at. So when collider.size.x
returns 14, it means that the collider's width is 14 units. This may be around 120 pixels on your display, on others it may be 60 or anything, in fact. All measurements, distances and positions are measured in terms of units, which is why you shouldn't really need to convert to other units, like pixels.
If you want to convert the units to pixels, it involves quite a bit of magic and math. Since your game is 2D, the orthographic projection makes it much easier to do that, but still it ain't a trivial task. If you are really sure that you need to use pixels as a unit, then only will I outline the process, it'll take me some time to come up with it too. If you aren't sure whether you need to use pixel units, post what you aim to do on here, we can help you out.
Well the reason why I wanted to use pixels is because I am only doing a 2D game. $$anonymous$$y tiles are 50x50 tiles, so I would need to place them each 50 pixels to the right everytime I load a new tile however Unity restricts this because I cannot use pixels.
Your answer
Follow this Question
Related Questions
Canvas match width or height 1 Answer
How to get 2d camera width & height? 2 Answers
Getting Child Box Collider2D Size 0 Answers
Find the Terrain Width and Height 1 Answer