How do i get some objects to ignore collision with a specific object?

I’m making a 2D platformer, and my player can shoot projectiles. I don’t want the projectiles to be able to collide with the player, have tried some different things, but can’t get it to work. Can anyone tell me how to accomplish this?

You could use the Layer Collision Matrix from Edit > Project Settings > Physics to do that without scripting.

Create a layer (e.g Layer1) and disable the Layer1/Layer1 collision. Also, assign your objects to this layer, of course.

alt text

It’s very simple.

Simply take the object(s) that you want to be ignored, and give them a tag, or layer. Now the method of exactly “how” to ignore them is different based on what you are doing, but all you need to do is tell the object that the script is attached to (the one you want to be the “ignorer”, to do so when it encounters any object with the tag, or layer you set up.

If using a tag:
void OnCollisionEnter(Collider collision)
if(collision.gameObject.tag == “theobjectToIgnore” )

If using a layer:
void OnCollisionEnter(Collider collision)
if(collision.gameObject.layer == “theobjectToIgnore” )

and I would make sure that object to be ignored has a rigid body.

You are not supposed to put the IgnoreCollision on OnCollisionEnter as it will still detect the first ever collision, but you can put it on the start() function like this:

private void Start()
         GameObject player = GameObject.FindGameObjectWithTag("Player");     
         Physics2D.IgnoreCollision(player.GetComponent<Collider2D>(), GetComponent<Collider2D>());

So, first you assign an object to the variable player, here I’m assigning it with the object with the tag “Player” then after that you just use the IgnoreCollision function like usual.

For 2D, you need to put the objects on separate layers. Then you can either set the collision matrix in:

 Edit > Projects Settings > Physics2D

Or you can use Physics2D.IgnoreLayerCollision()

More info on the Physics2D Manager:

So I have tried the following as suggested.

void OnCollisionEnter(Collision collision)
      if (collision.gameObject.tag == "theobjectToIgnore")
          Physics.IgnoreCollision(theobjectToIgnore.collider, collider);

I’m trying to push a ball through a barrier, the ball is supposed to go through but not the character. The object is named Ball and the tag is also named Ball. So my script is as follows:

void OnCollisionEnter(Collision collision)
        if (collision.gameObject.tag == "Ball")
            Physics.IgnoreCollision(**Ball**.collider, collider);

the second ball though is red and it is not being detected via typing… can anyone help me?

For detecting and comparing two layers at runtime in a collision detector, I wrote the following extension:

public static class UnityExtensions {   
    public static bool ContainsLayer(this LayerMask mask, int layer) {
        return ((1 << (int)layer) & (int)mask) > 0;

You can then use that in a script with a collision handler like so:

using UnityEngine;
public class MyCollider : MonoBehaviour {
    [SerializeField] private LayerMask m_Layers;

    // should work for ANY trigger or collision detector.
    private void OnTriggerEnter2D(Collider2D other) {
        if (m_Layers.ContainsLayer(other.gameObject.layer)) {
            // ignore collision or whatever you want.

From there, attach the script to your collider and then, in the editor, populate the layers field through the layers dropdown and you’re good to go!

The Collision Ignoring aspect of this question has been pretty well covered, I think, and it has been really helpful in its own right. I would just like to point out that, for the purpose of making sure a bullet/projectile doesn’t interfere with your player upon firing, you could originate the bullet from slightly in front of the player (or slightly outside the player’s space as appropriate to the direction of travel). If you have situations in which the player could come back in contact with the bullet, the various collision ignoring methods here should handle it.

my recommendation would be to set individual layers for specific objects and then use layers to ignore each other. (so like default being 0 and transparent being 1 ect)

for 3D:
Physics.IgnoreLayerCollision(9, 10);
for 2D:
Physics2D.IgnoreLayerCollision(9, 10);

just switch out 9, 10 for the 2 layers you want to ignore each other.

Set Game Objects Collider “isTrigger” to true and use:

        private void OnTriggerEnter2D {

             if ("ObjectToRemove")) {

                print("ObjectToRemove Collided");


You can also use IgnoreLayerCollision.

Physics.IgnoreLayerCollision(8, 10);

This would ignore collisions between layers 8 and 10.
Just make sure that when you are making 2D game you are using

Physics2D.IgnoreLayerCollision(8, 10);