Search Unity

2D Physics, Ignore Collision not working

Discussion in 'Physics' started by NickWinters, Feb 6, 2017.

  1. NickWinters

    NickWinters

    Joined:
    Dec 6, 2014
    Posts:
    23
    I have the following code, that's designed to create a missile and launch in the direction of my spaceship.

    public void FireMissile()
    {
    GameObject missile = Instantiate(Missile, transform.position, transform.rotation);
    Physics2D.IgnoreCollision(GetComponent<Collider2D>(), missile.GetComponent<Collider2D>());
    }

    However, it doesn't seem to ignore the collision. Here's a video of what happens:


    Is this as expected? Do I not understand what IgnoreCollision is supposed to do? How should I debug this? Both the ship and the missile have polygon collider 2ds. They also have RigidBody2D's of body type Dynamic.
     
  2. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,415
    I can only assume that the GameObject that the above script is on has only a single collider i.e. the one returned in 'GetComponent<Collider2D>()' (worth debugging this and ensuring it's not null) and that the 'Missile' prefab also only has a single collider. Also, the above code assumes that the script is on the same GO as the collider and that the missle is the same.

    Finally, both GO must be active/enabled. If not, collisions cannot be ignored. If you disable a GO then the setting is lost.
     
  3. NickWinters

    NickWinters

    Joined:
    Dec 6, 2014
    Posts:
    23
    It turns out I had two, identical Collider2D on the space ship. Debugging....
     
    MelvMay likes this.
  4. SwixDevs

    SwixDevs

    Joined:
    Apr 19, 2019
    Posts:
    6
    So, if there are 2 colliders on 1 gameobject, them, IgnoreCollision won't work? Please reply, I know this ia an old thread...
     
  5. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,415
    If you have two Collider2D on the same GameObject then it means they're attached to the same Rigidbody2D therefore they'll never contact each other anyway so your question doesn't really make sense. Only colliders attached to different Rigidbody2D contact each other.

    That's how most physics systems work.
     
  6. NickWinters

    NickWinters

    Joined:
    Dec 6, 2014
    Posts:
    23
    @ShauryaJuneja I had two colliders on a single game object (the spaceship). GetComponent<Collider2D> only returned the first one, not the second one. So it ignored the collision between the missile's collider and the spaceship's first collider. It didn't ignore the collision between the missile's collider and the spaceship's second collider, since I never told it to ignore the collision.
     
  7. chauhanravi011

    chauhanravi011

    Joined:
    Jul 19, 2022
    Posts:
    1
    use composite collider 2D on top of other colliders, make sure it si the first collider in the inspector. it will combine all other colliders into one.
     
  8. Ktari

    Ktari

    Joined:
    Jan 26, 2024
    Posts:
    1
    i have the same problem
    however the only 2 solutions is to detect the missile collision with the ship and call the function Physics2D.IgnoreCollision
    where the collision-ed object collider should be col.collider in the function
    this way will work like a magic but the most annoying thing is, it seemed to collide anyway in the first frame when the missile instantiated, unfortunately

    another way is by using layers where you can use the function Physics2D.IgnoreLayerCollision
    this will work pretty easy but you need to make Missile Layers and The ship Layer seperately in the scene
    for example we have Missiles in the Layer 6 and the Player in the Layers 7
    Physics.IgnoreLayerCollision(6,7)
    will work perfectly for you
    here is my Code
    Code (CSharp):
    1. public GameObject Player;
    2. float speed;
    3.  
    4. void Awake()
    5.     {
    6.         Physics2D.IgnoreLayerCollision(8,9);
    7.     }
     
  9. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,415
    Because you get a collisions callback to tell you that the collisions has happened, not that it will happen. You're asking for it to not collide after it already has.