OnCollisionEnter2D not getting called
I have two objects. both have Box Collider 2D and Rigidbody 2D with these attributes (they are the same):
Box collider:
Material: none ,
IsTrigger: checked ,
Used By Effector: Unchecked,
Offset X:0 , Y:0
Size: x:1.961539 y:2.538462
Rigidbody: isKinematic : checked (i've tried all combinations of checked and unchecked between the two objects)
Interpolate :none
sleeping mode: Start Awake
Collision Detection: continuous (i've tried all combinations of discrete and continuous between the two objects)
On one of the objects I have this code:
function OnCollisionEnter2D(col : Collision2D) {
Debug.Log("collide");
if(col.collider.tag == "Objecttag"){
Debug.Log("collide");
}
}
However no matter what objects collide (which is why I have one outside the if loop, and one inside) I never get any output to the console. What could be wrong?
Answer by vintar · Aug 01, 2016 at 08:17 PM
Collision will not be detected if isTrigger is checked.
ooops! I honestly couldn't think of the reason until I saw this. Thank you.
Answer by Unessential · Aug 01, 2016 at 08:24 PM
I intend to have the two bodies be able to overlap each other (hold the same space) on the screen and enter a specific state while it's held in this position. Also count how many times this state has been entered. How do I do that? If i uncheck "istrigger" the bodies literally collide and prevent each other from occupying the same 2D space. and if i code one body to enter the other it gets "ejected" out.
That is the situation when both bodies have "is trigger" checked (and the console prints). However, when one of them is checked and the other isn't. (or neither is checked) they are able to pass through each other again, but the console won't print.
tl;dr, how do I have detection as well as being able to have both objects hold the same space?
After googling more about triggers I found that the constructor is different:
function On Trigger Enter2D(col : Collider 2D) {
$$anonymous$$y code is now:
function OnTriggerEnter2D(col : Collider2D) {
Debug.Log("collide1");
if(col.gameObject.tag == "objecttag"){
Debug.Log("collide2");
}
}
I've also numbered the two console prints to differentiate with them. Right now I can get 1 to fire but not two.