thisCollider and otherCollider from collision.GetContacts()
Is there a fixed definition, which collider is thisCollider and which is otherCollider when retrieving ContactPoints?
The Documentation for ContactPoint.thisCollider says that thisCollider is "The first collider in contact at the point." Does this mean, that it is the collider on which the collision was noticed by the physics system or is it actually the collider, on which OnCollisionEnter() is called right now? OnCollisionEnter() of course is called on both colliders, so the question is, whether thisCollider and otherCollider are automatically swapped on the second call?
My background is, that I am defining OnCollisionEnter() on a GameObject and need to retrieve the collider which got into contact with the collider of my GameObject. So I ask myself, whether it is enough to use the otherCollider, or if I have to make sure, that the otherCollider is not the collider of my GameObject.
Answer by LevBronstein · May 04, 2021 at 10:37 PM
I've recently ran into the same kind of situation. So, I made some empirical observations.
OnCollisionEnter is called for each new collision on a each GameObject (this complies to the documentation). Basically, when you have a single Collider for a single GameObject, OnCollisionEnter is called for each GameObject, even if collision points are same (equal by their coordinates). If you have multiple Colliders attached to a single GameObject, OnCollisionEnter is called once, where the physics engine returns ContactPoint, each with a different otherCollider. Finally, thisCollider always returns current GameObject's (to which this running script is attached), so, if you have multiple colliders on the current GameObject, I believe, it returns the closest (to the collision point) one. However, in this case the physics engine returns kind of unstable results - sometimes I'm getting OnCollisionEnter for each Collider attached to my current GameObject, sometimes - I don't. Maybe this depends on Collider's type. Also, I think, this is the case, when you should consider your other object's OnCollisionEnter data.
Yes I also figured out that thisCollider always refers to the object OnCollissionEnter was called on. Thank you for sharing your observations.
Your answer
Follow this Question
Related Questions
Why is my trigger collider acting like a normal collider? 1 Answer
This wont work with a Character Controller. 0 Answers
Question on Physics.Simulate Behavior 0 Answers
Block Issues 1 Answer