Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 loljoch · Aug 13, 2020 at 08:55 PM · collisionphysics2dcollision detectioncollider2dcollisions

My OnCollisionEnter says i have 128 Collisions every collision

So I have a Rigidbody2D and a 2D Capsule collider on my object. I also have a script with

 private void OnCollisionEnter2D(Collision2D collision)
     {
         List<ContactPoint2D> _contactPoints = new List<ContactPoint2D>();
         collision.GetContacts(_contactPoints);
 
         Debug.Log(_contactPoints.Count);
 
         for (int i = 0; i < _contactPoints.Count; i++)
         {
             //Checking some stuff
         }
     }

The problem is that my _contactPoints List always has a count of 128, which includes the collisions they are making right now, but the rest are just collisions with point(0, 0). This means that most of the time my loop is aimlessly iterating over 126 fake collisions.

I have only thought up the solution of deleting the other collisions before the loop, but this just feels like a quick fix.


Does anyone know any way to get the accurate number of collisions from the collision parameter within OnCollisionEnter2D?

Comment
Add comment · Show 1
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 CodesCove · Aug 14, 2020 at 12:12 AM 0
Share

I'm not sure what you mean by number of collisions but Collision2D.contactCount (from ver 2018.2) gives the number of contacts in the OnCollisionEnter2D collision. Also the list you give as a parameter might not work as intended. You can use contactCount to deter$$anonymous$$e and set the size of list before passing it to the GetContacts method.

1 Reply

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

Answer by Spip5 · Aug 14, 2020 at 12:00 AM

Can you try this ?

     private void OnCollisionEnter2D(Collision2D collision)
     {
         ContactPoint2D[] _contactPoints = collision.contacts;
  
         Debug.Log(_contactPoints.Length);
 
         for (int i = 0; i < _contactPoints.Length; i++)
         {
             //Checking some stuff
         }
     }

Comment
Add comment · Show 2 · 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 loljoch · Aug 14, 2020 at 01:29 PM 0
Share

Thank you, that works and for anyone wondering how the code looks like now:

     private void OnCollisionEnter2D(Collision2D collision)
     {
         ContactPoint2D[] _contactPoints = new ContactPoint2D[collision.contactCount];
         collision.GetContacts(_contactPoints);
 
         Debug.Log(_contactPoints.Length);
 
         for (int i = 0; i < _contactPoints.Length; i++)
         {
             //Checking some stuff
         }
     }

I changed collision.contacts to GetContacts(), since Unity recommends that

avatar image Spip5 loljoch · Aug 14, 2020 at 04:40 PM 0
Share

Alright, i didnt know it's recommended, glad that works :).

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

206 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

Related Questions

Problem with method Collider2D.isTouchingLayers() 4 Answers

How to make different actions using OnMouseDrag with two different colliders on one Game Object. 0 Answers

Physics2D bug or something I can avoid? 3 Answers

Bullets go through collider even with continuous collision detection! 0 Answers

How do I stop characters from standing on top of each other 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