Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 /
avatar image
2
Question by wanobano · Feb 13, 2015 at 12:13 PM · physics2dcollider2dboxcollider2doverlap

Physics2D.OverlapArea inaccurate?

In my scene there are two GameObjects. Both with a BoxCollider2D. The left one has a script attached which uses the Physics2D.OverlapArea method to find collisions between the two objects. As parameter of the method I use the values of bounds of the BoxCollider2D. I log the maximum X of the collider (the right edge). If there is a collision I also log the minimum X (the left edge) of the other collider. You can see in the log that there is an overlapping recognized with an X-value of the right edge of the left object smaller than the left edge of the right object. How is that possible? The two colliders should not overlap.

In the screenshot you can see the small gap.

The overlapping code:

 void Update() {
         Collider2D result = Physics2D.OverlapArea(collider2D.bounds.min, collider2D.bounds.max, collidingLayer);
 
         Debug.Log("Player maxX: " + collider2D.bounds.max.x);
 
         if (result != null) {
             Debug.Log("Block minX: " + result.bounds.min.x);
         }
     }

alt text

overlap-test.png (74.2 kB)
Comment
Add comment · Show 6
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 Baste · Feb 13, 2015 at 01:11 PM 1
Share

"The left one has a script attached which uses the Physics2D.OverlapArea method to find collisions between the two objects"

why? You have colliders. Why not just have them collide? The collision is already happening in the physics engine, so you're recalculating information that's already there for you to pick up if you just add an OnTriggerEnter2D

avatar image meat5000 ♦ · Feb 13, 2015 at 01:24 PM 0
Share

You are using it wrongly. Read the docs.

It wants opposing corners, not edges. $$anonymous$$in and $$anonymous$$ax are Depth parameters.

avatar image wanobano · Feb 13, 2015 at 03:15 PM 0
Share

Baste: I don't use the built-in physics for colliding since my GameObject has multiple colliders, each colliders bounds colliding on a different layer (by script). Otherwise I can only deter$$anonymous$$e one layer per GameObject. The colliders don't use their "Is Trigger" ability. I usecolliders here to visualize the colliding bounds and to be able to change them by just modifying the according collider bounds.

meat5000: The docs say: "The rectangle is defined by two diagonally opposite corner coordinates in world space. You can think of these as top-left and bottom-right but the test will still work if the ordering of the points is reversed." So what's wrong with the parameters? I tried the following code earlier but it also has the same result as above.

 void Update() {
         Vector2 topLeft = new Vector2(
             collider2D.bounds.center.x - collider2D.bounds.extents.x,
             collider2D.bounds.center.y + collider2D.bounds.extents.y);
 
         Vector2 bottomRight = new Vector2(
             collider2D.bounds.center.x + collider2D.bounds.extents.x,
             collider2D.bounds.center.y - collider2D.bounds.extents.y);
 
         Collider2D result = Physics2D.OverlapArea(topLeft, bottomRight, collidingLayer);
 
         Debug.Log("Player maxX: " + collider2D.bounds.max.x);
 
         if (result != null) {
             Debug.Log("Block $$anonymous$$X: " + result.bounds.$$anonymous$$.x);
         }
     }



avatar image meat5000 ♦ · Feb 13, 2015 at 05:14 PM 1
Share

In your text you said left and right edge :)

But your corner vectors look correct, only you use bounds max and $$anonymous$$ to debug, which might not be the same.

Debug.Log all the corner points of the offending colliders and see if this holds up.

I dug out an interesting post.

http://answers.unity3d.com/questions/37412/controllerbounds$$anonymous$$max-is-giving-me-a-value-i-have.html

avatar image hexagonius · Feb 13, 2015 at 11:03 PM 0
Share

Is the distance between the objects greater than 4.381 Units? meat5000 is correct. the $$anonymous$$s and max would be the same if the objects were in the same spot.

Show more comments

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by hexagonius · Feb 17, 2015 at 12:45 PM

I know from a project that 2D colliders use some kind of a skin like the charactercontroller does. By zooming in on them I found it round about 0.03 units thick. Maybe thats also the case here. Why are you not using OnCollisionEnter2D?

Comment
Add comment · Show 3 · 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 wanobano · Feb 17, 2015 at 03:25 PM 0
Share

Is this skin around physic bounds documented anywhere? I don't use the built-in mechanism because I use multiple bounds/colliders for different layers on one single GameObject (see my comment above)

avatar image meat5000 ♦ · Feb 17, 2015 at 03:32 PM 0
Share

What is your Penetration value in your physics manager? Is it negative?

avatar image wanobano · Feb 17, 2015 at 07:30 PM 0
Share

No, the value is at its default (0.01). I did not change any physics settings.

avatar image
0

Answer by wanobano · Feb 17, 2015 at 08:03 PM

May the reason be that the physics calculation is operating at a lower resolution (maybe for performance issues) than the world resolution???

Comment
Add comment · 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

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Physics2D.OverlapBoxNonAlloc not return all overlapped collider 1 Answer

How to Detect if a Collider is in the Area, Move Away, Rinse and Repeat? 1 Answer

2D Box Colliders Overlapping On Collision 4 Answers

Physics2D.OverlapCircleAll and OverlapCapsuleAll behaves differently? 0 Answers

Physics2D.OverlapBoxAll and Physics2D.OverlapAreaAll are inaccurate. 0 Answers


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