- Home /
OnTriggerEnter2D not detecting tilemap colliders
I have a simple topdown game where the player can shoot bullets. To detect if it's hit an enemy, I use the following code and sure enough it works.
private void OnTriggerEnter2D(Collider2D col)
{
if ( col.tag == "Enemy Body" )
{
enemyHit = true;
col.gameObject.GetComponent<Enemy>().Damage(bulletDamage);
}
}
When creating levels, I decided to use tilemaps, with walls tagged as Wall
. It has a Tilemap Collider Component. The problem is when I try to add wall detection to my previous script, it doesn't detect it. My new code is:
private void OnTriggerEnter2D(Collider2D col)
{
Debug.Log(col);
if ( col.tag == "Enemy Body" )
{
enemyHit = true;
col.gameObject.GetComponent<Enemy>().Damage(bulletDamage);
}
else if ( col.tag == "Wall")
{
Destroy(this.gameObject);
}
}
I also added a debug log at the beginning to check if the event actually triggers, but it only ever logs collision with enemies. I tried the following to no avail:
set isTrigger to wall (which just allowed my other sprites to pass through
set isTrigger to bullet
add a RigidBody component to the wall
I've ran out of ideas and the answers in all the similar problems I've found online still don't work for me.
Answer by sachinchetu · Aug 05, 2020 at 05:36 PM
Have you enabled Kinematic in Rigidbody component? Please check once, if it is enabled no physics works on that object.
You can also put your second condition in another If block to test whether it is wrong something in code or Unity side.
Answer by mbro514 · Aug 05, 2020 at 09:42 PM
Also, make sure that your "Wall" tilemap is tagged with "Wall".
Answer by VarelaByakko · Jan 22, 2021 at 12:06 AM
Check your Physics 2D in Project Settings, you might have used Layer Collision Matrix for enemie's layer only with bullet collision.
I got the same problem with ground and wall, and I fixed by checked "EnemyProjectile" with "Ground" (a Tile's layer) in Layer Collision Matrix