- Home /
How to get GameObject with its tag when ignoring Collision?
Hey guys!
In my game I have a player who shoots a bullet and an enemy who is patrolling around. The enemy patrols between two empty gameobjects which mark the range of the patrol. The problem is that these objects must have box colliders so that the enemy stays within this range. They also have a tag. I have Collider2D on my bullet in the OnTriggerEnter2D function.
Now I found this option so I can let the bullet ignore the gameobjects but in the ignoreCollision function I need to refer to the gameobject. How do I get it? What should I put instead of "theobjectToIgnore" in the physics function? I hope you can help me out and kind regards!
if (hitInfo.gameObject.tag == "theobjectToIgnore")
{
Physics.IgnoreCollision(theobjectToIgnore.collider, hitInfo);
}
Answer by CodesCove · Dec 03, 2021 at 10:40 AM
Physics.IgnoreCollision is meant for the cases where you cannot separate the two colliders to a different physics layers in a meaning full way. In you case this is not so.
For your use case it's better to set the bullets and colliders that you don't want to collide to separate physics layers.
In the Inspector set the objects with the collider to different Layers (you probably need to create new layers). Then verify that their relation is unchecked in the Project Settings --> Physics --> Layer Collision Matrix
After that you will not get any collision between colliders in those layers.