Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by FearMe · Mar 24, 2016 at 02:25 AM · coordinatesboxcollider2drectheightwidth

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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

Comment
Add comment · Show 5 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image FearMe · Mar 24, 2016 at 06:12 PM 0
Share

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?

avatar image EvilTak FearMe · Mar 25, 2016 at 09:10 AM 0
Share

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.

avatar image FearMe EvilTak · Mar 25, 2016 at 01:30 PM 0
Share

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.

Show more comments

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

51 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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

RectTransform - how to change height and width 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges