- Home /
Collider2D.GetContacts(List() contacts) works with OnColliderEnter2D but not with OnTriggerEnter2D
I need to know the point of contact when two objects Collide, but I want them to pass through each other. I originally tried:
private void OnCollisionEnter2D(Collision2D collision)
{
Debug.Log("Collided");
List<ContactPoint2D> contacts = new List<ContactPoint2D>();
int numContacts = collision.GetContacts(contacts);
Debug.Log(numContacts);
}
The program would then write "Collided" and "1" to the console when the players bumped into each other but didn't allow for them to pass through each other.
Then I switched on the "IsTrigger" value on my player and tried again with the following code:
private void OnTriggerEnter2D(Collider2D collision)
{
Debug.Log("Trigger");
List<ContactPoint2D> contacts = new List<ContactPoint2D>();
int numContacts = collision.GetContacts(contacts);
Debug.Log(numContacts);
}
The program would then write "Trigger" and "0" to the console when the players bumped into each other but did allow for them to pass through each other
Does the OnTriggerEnter2D() function not track the ContactPoint2Ds? Is it because OnTriggerEnter2D has an input of Collider2D and not Collision2D?
Does anyone know why this is happening? Thanks!
Answer by SlowCircuit · Sep 21, 2020 at 11:58 PM
Unfortunately OnTrigger methods don't get contact information. The workaround is to instead have the trigger object in question use a kinematic rigidbody and set isTrigger to false.
Your answer
Follow this Question
Related Questions
2d rigidbody falling from the collider when it is moved 3 Answers
Object hits another but doesnt bounce off again. 0 Answers
Small 2D collider passing through other thin collider 2 Answers
Blocking object from going through colliders (no rigidbody) [2D] 1 Answer
Using child colliders with rigidbodies/joints in 2D 0 Answers