• 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 frederic_unity363 · Jan 18, 2019 at 07:18 AM · collisiongameobjectobject

Collider2D.GetContacts() always returns only 1 contact

I am trying to get the contact points of two 2D Colliders (circle and box) which are moving towards each other and eventually overlap. The Maximum Amount of Contact Points would be 8, So I created an Array to store the results which is greater than 8.

 private ContactPoint2D[] contacts = new ContactPoint2D[10];
 
 void OnCollisionStay2D(Collision2D collision)
     {
         collision.GetContacts(contacts);
  
         foreach (ContactPoint2D contact in contacts)
         {
             Vector2 hitPoint = contact.point;
             Debug.Log(hitPoint.ToString());
         };
 }

visual of the two objects

Now I get as a return only one contact point which seems to be a point in the middle or an average value of the overlapping area.

Is this a normal behaviour? How do I get all points of intersection of the Colliders?

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 xxmariofer · Jan 18, 2019 at 09:06 AM

That object will only have 2 contact points, that are the points were both intersect, if you are getting one is becaouse one (or both) objects are being pushed away and they are only contacting once, force the rigidibodys to freeze position so that they dont move and you will get both contact points.

Comment
Add comment · Show 6 · 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 frederic_unity363 · Jan 18, 2019 at 09:16 AM 0
Share

I am moving the objects in the same script with the update function

     void Update()
     {
         if (counter <= 200)
         {
             transform.Translate(new Vector3(x, y, z) * Time.deltaTime, Space.World);
         }
     }

I added a counter which is initally 0 and starts counting as soon as the elements touch each other, to be able to stop moving the elements.

the updated Collision function is:

  private ContactPoint2D[] contacts = new ContactPoint2D[10];
  
  void OnCollisionStay2D(Collision2D collision)
      {
          counter += 1;
          collision.GetContacts(contacts);
   
          foreach (ContactPoint2D contact in contacts)
          {
              Vector2 hitPoint = contact.point;
              Debug.Log(hitPoint.ToString());
          };
  }

But still, after stopping the movement, I only get 1 contact point when the position where the movement stopped should have two

avatar image xxmariofer frederic_unity363 · Jan 18, 2019 at 09:30 AM 0
Share

Can you please Freeze position of the rigidbodys? Then there will be 2 contacts. the exact same second the object stops moving will the one with rigidbody (or both) be pushed outside since rigidbody not trigger cant overlap, and thats why they will only be one colllisiom.

avatar image frederic_unity363 · Jan 18, 2019 at 09:35 AM 0
Share

Aaah you mean for example make it kinematic?

I updated to :

     void Update()
     {
         if (counter <= 200)
         {
             transform.Translate(new Vector3(x, y, z) * Time.deltaTime, Space.World);
         }
         else
         {
             this.gameObject.GetComponent<Rigidbody2D>().is$$anonymous$$inematic = true;
         }
 
     }


but still, only one contact point. This script is active for both objects, so both objects will be kinematic after touching and stopping their movement.

avatar image xxmariofer frederic_unity363 · Jan 18, 2019 at 09:52 AM 0
Share

No no, i mean Freeze the constrains in the editor, you will be able to move the object will translate still but freeze position x and y from the editor (in the rigidbody2d) that will force the objects to overlap when you stop moving them

avatar image frederic_unity363 xxmariofer · Jan 23, 2019 at 01:35 AM 0
Share

I also tried it with a third element. I received then 3 contact points where I expect 4. Also kind of randomly positioned.

  • You can see 4 different contact points but each element has only three though.

three elements

avatar image frederic_unity363 · Jan 21, 2019 at 12:41 AM 0
Share

I tried it this morning, stop moving and freezing both X / Y Position of both objects and with a second try only of one object and still receive only one contact point.

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

184 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

Related Questions

How do I track an object of a specific type at collision? 0 Answers

If two of the same objects spawn on top of each other, is it possible to destroy only one? 0 Answers

OnTriggerEnter not working, tried everything! :( (C#) 3 Answers

Why do Instantiated GameObjects Colliders only work on player i am controlling,nothing else? 2 Answers

Transform.LocalScale Script Problem. Please help. 2 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