- Home /
OnCollisionEnter2D not being called on entering new tile once already called.
the title basically explains the issue- it seems that when adjacent both have onCollisionEnter2D when the object moves across them it only detects the point of entry - it does not detect oncollisionexit2d, the tilemap in question does have tilemap collider2d, and is used by collider.
However i found this https://answers.unity.com/questions/262073/oncollisionenter-only-detects-once.html
Which then had me go edit the tiles collision box in sprite editor's physics shape, making it a px smaller so that there would be a no collision space in between the tiles - however it STILL does not get detected. I dont see why. Maybe the fact the sprite's collider/rigidbody is more than 2px wide and it think its still in the same? But that shouldnt be it.
Here's the code
void OnTriggerEnter2D(Collider2D col)
{
if (col.GetComponent<Player>())
{
int randNum = Random.RandomRange(0,100);
Debug.Log(randNum);
if (randNum >55) {
Debug.Log("Enter Tile Working?");
//code here
}
}
}
void OnTriggerExit2D(Collider2D col)
{
if (col.GetComponent<Player>()) {
// Nothing
Debug.Log("exited tile?");
}
}
Anyone have a fix?
Answer by Atiyeh123 · Sep 04, 2018 at 09:46 AM
Hi @fr0stsp1k3 I've tested your issue an it detects all collisions in my unity 2018 version. I've attached the test project below: link text
The problem isnt that it doesnt detect, it detects all my collisions too - the problem is that it doesnt detect a "new" collision on adjacent tiles. I.E "i move from one grass tile to another" it doesnt detect that as a new tile/collision