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 /
avatar image
0
Question by Dalsia · May 28, 2020 at 09:16 PM · collisioncollision detectioncollision2d

Discrepancy with ColliderDistance2D "minimum separation" distance

Maybe I'm just doing something wrong, but I'm unsure of what's causing this issue.

I have a few, simple lines of code (lines 24 and 27) that are behaving differently on sloped surfaces depending on how far into the sloped surface my character is sitting.

 [SerializeField] private LayerMask collisionMask;
 
 private BoxCollider2D boxCollider;
 
 private void Awake()
 {
     boxCollider = GetComponent<BoxCollider2D>();
 }
 
 private void Update()
 {
     DetectAndResolveCollision();
 }
 
 private void DetectAndResolveCollision()
 {
     // Get the collision/overlap
     Collider2D collision = Physics2D.OverlapBox(transform.position, boxCollider.size, 0, collisionMask);
 
     // Get the collision distance info
     ColliderDistance2D colliderDistance = collision.Distance(boxCollider);
 
     // Find the shortest resolution vector
     Vector2 resolutionVector = Mathf.Abs(colliderDistance.distance) * colliderDistance.normal;
 
     // Resolve the collision by moving out of it by the shortest amount
     transform.Translate(resolutionVector);
 }


I've already discovered a superficial reason for my problem that will hopefully be better explained by this picture:

alt text
What I've noticed is that if only one corner of the character's collider is in the slope, the resolution occurs as expected (as shown in the top half of the picture). However, if more than one corner of the character's collider is in the slope, the shortest resolution is defined as the distances from the center of some side/face of the box collider to its closest point on the outside of the slope. Therefore, if 3 corners are in the slope, the distance from the center of two sides/faces of the box collider to their closest points outside the slope will be compared, and the shorter distance will be used in the "resolution" by pushing this center point out of the slope. The problem, of course, is that the entire collider is not pushed out of the collision when this happens, so I would have to run the collision check a second time, which seems like an incorrect approach.

Why is this happening? I thought ColliderDistance2D - and, more specifically, Collider2D.Distance, which returns a ColliderDistance2D object - was always supposed to give you the shortest distance that moves the ENTIRE collider out of its collision. The Unity docs even say that Collider2D.Distance "calculates the minimum separation of this collider against another collider." How can the "minimum separation" be a value that doesn't fully separate the colliders?

I've already been using trigonometry to figure out a lot of different ways to resolve collisions, and I know I can do that here too, but I was hoping to overcome this discrepancy with a built-in solution that already exists in Unity. I would appreciate any help or explanations that can be offered.

mbysz4n.png (36.6 kB)
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

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by Dalsia · May 29, 2020 at 09:49 PM

I also asked this question on the Unity forums, and it does seem that there is a discrepancy as described. Here's a link to the thread with the official dev answer: https://forum.unity.com/threads/discrepancy-with-colliderdistance2d-minimum-separation-distance.900389/

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
avatar image
0

Answer by Map-Builder · May 28, 2020 at 11:13 PM

Not sure to understand everything, but physics are supposed to be fast so you may not expect too much. but physics are mostly calculated for 1 corner. And results may be bad for 2 (fast object, or face against face). but in case down left, since both corner are in, full edge is inside so it takes the middle of it IMO. You may try different method or do it your self manually. Physic engine do not need this value for (in 3d) a triangle fully inside another collision) But since you know it's in the middle of the edge...


But your stuff seem simple, use circle colliders and they is no "corners" so you'll get a correct distance with edge.


I'm a designer, and I think that when things are not working with a tool you don't know, either understand or change your method.

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

197 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 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 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 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

Sprite ignoring collison 1 Answer

if statement not working when detecting collision between two prefabs 1 Answer

How to set collision for an object with specific collider size? 3 Answers

Disable collision before collision actually happens 1 Answer

Rigidbody gets stuck in wall after falling 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