- Home /
2D Colliders do not register collision
I have a few walls in my game tagged as "Obstacle" and have a 2D box collider attached to them. I instantiate a monster prefab that is supposed to change it's direction when hitting one of the walls. The monster has a 2D box collider, a movement script and a 2D Rigidbody attached to them. The monster also has an empty GameObject called frontCheck.
Here's the code for collision detection:
Collider2D[] frontHits = Physics2D.OverlapPointAll (frontCheck.position, 1);
Debug.DrawLine(transform.position , frontCheck.position , Color.red, 0.5f, false);
foreach(Collider2D c in frontHits){
if(c.tag == "Obstacle"){
Debug.Log("Hit");
break;
}
}
For some reason there's never any collisions detected and "Hit" is never printed. Any idea why this is happening?
Comment
Answer by StevenWendel · Mar 06, 2014 at 02:49 AM
Try removing the layermask parameter of 1 so it's not just looking in that specific layer.
Collider2D[] frontHits = Physics2D.OverlapPointAll (frontCheck.position);