- Home /
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());
};
}
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?
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.
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
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.
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.
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
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.
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
Follow this Question
Related Questions
If two of the same objects spawn on top of each other, is it possible to destroy only one? 0 Answers
How do I track an object of a specific type at collision? 0 Answers
Physics.OverlapSphere() not working after using pool of objects 0 Answers
how to make a script when u collide with an object it disappears 3 Answers
My marble (Player/Gameobject) goes through the "play board" (gameobject). Why? 1 Answer