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
1
Question by PlayCreatively · Mar 14, 2020 at 06:30 PM · 2dcollision detectionintersectionignore collisions

How to ignore collisions between two objects but still get the notification?

Hi, I'm working in a 2D environment and I've got items that my character can drop. The character drops the items inside himself, which is why I want to ignore that collision, but I want to be notified when their colliders stop intersecting each other to then turn the collision back on. As of now the collision is ignored for a specified time, which is what most people suggest on the web, but that's not good enough. Anyone got any suggestions? Thanks in advance.

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

3 Replies

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

Answer by PlayCreatively · Mar 15, 2020 at 12:51 AM

@metalted Hit the nail on the head with his collider distance solution, however it would not always be 100% accurate for complex colliders and calculating the distance when taking into consideration each collider's rotation would add allot of complexity, However when I was further investigating collider distances I found that unity already has a function for that Physics2D.Distance(col1, col2) and that returns a handy ColliderDistance2D struct that holds this also very handy bool: isOverlapped!


So because Physics2D.IsTouching(col1, col2) doesn't detect ignored collisions you'll have to use Physics2D.Distance(col1, col2).isOverlapped instead.

So to ignore collisions between two objects while they're overlapping just use this simple coroutine:

 IEnumerator IgnoreCollision(Collider2D col1, Collider2D col2)
 {
         Physics2D.IgnoreCollision(col1, col2, true);
         while (Physics2D.Distance(col1, col2).isOverlapped) yield return null;
         Physics2D.IgnoreCollision(col1, col2, false);
 }

Hope this helps future browsers.

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
1

Answer by metalted · Mar 14, 2020 at 08:36 PM

As you know how to toggle the collision over time, maybe you could toggle it over distance.


By saving the transform of the instantiated drop, you can later use Vector3.distance to get the distance between the player and the drop. The distance needed to clear the colliders would be : collider1.bounds.size.x / 2 + collider2.bounds.size.x / 2. This only works for spheres, so unless you are making pac-man, you might want to play around with these values. There is a lot of info to be found about collider bounds and collider extents. Eventually, when the distance threshold is reached, toggle the collision.


If you want to be sure that the collider is cleared, you could calculate the diagonal between the x-axis extents and the z-axis extents. This will give you a distance that, when drawing a line from the center of the object, will always be outside or on the collider in the x-z plane. You add the diagonal of collider 1 and 2 together to get the minimum clearing distance in the x-z plane. Then when measuring this distance, you should ignore the y-position of the colliders, to not add extra distance because of the height difference.

Comment
Add comment · Show 1 · 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 PlayCreatively · Mar 14, 2020 at 08:45 PM 0
Share

That's a brilliant suggestion -toggle it over distance Obviously won't be 100% accurate in all collider cases but it's far better than over time. Also rotation will add extra complexity but I can manage.

avatar image
0

Answer by 3kWikiGames · Mar 14, 2020 at 08:31 PM

Im not sure if I totally understand, but if I do then you could turn it into a trigger instead, and then turn this off whenever you need for it to become a collider again?

Comment
Add comment · Show 1 · 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 PlayCreatively · Mar 14, 2020 at 08:36 PM 0
Share

No I need to ignore specific collisions, not all collision. The problem with Physics2D.IgnoreCollision() is that I won't be able to detect those collisions anymore

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

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

Tile collision by sprite is not accurate with Tilemap Collider 0 Answers

Make 2D projectile collide only from the outside 1 Answer

(2D intersection+picture)How to show up the invisible object when intersect with another object. 1 Answer

Childs Collision Calls Parents OnTriggerEnter2D() 0 Answers

2D Collider seems bigger than it is 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