Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

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:
    10,623
    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:
    10,623
    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.