Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Physics2D.IgnoreLayerCollision();

Discussion in 'Scripting' started by larswik, Sep 21, 2017.

  1. larswik

    larswik

    Joined:
    Dec 20, 2012
    Posts:
    312
    I made some modifications to the layering last night to make my game more efficient but now it is acting strange. When using the IgnoreLayerCollision(12,24) on 1 single Prefab, will that set the matrix for all there other objects?

    Testing my game I have 2 cannon balls and 1 shield. 1 cannon ball can penetrate the shield and the other can not. I shot the first cannon ball at the shield and it hit the shield, as expected. Then I fired the one that can penetrate the shield and it went through as expected. But then when I fire the first one again, and used the cannon ball that should have hit the sealed, it went through, which it should not have.

    I am looking for any confirmation that when you use the IgnoreLayerCollision() in a script, that then changes the LayerCollision for all other GameObjects? So it's like a global LayerCollision adjustment instead of a local on specific to the gameObject in question?
     
  2. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    For sure, it's in the docs: https://docs.unity3d.com/ScriptReference/Physics2D.IgnoreLayerCollision.html
    In the (non-2D version), there is also a note about how this will reset the trigger states for OnEnter/OnExit.. which I would guess probably happens in 2D, also, but wasn't written down.

    In your case, if you have 2 types of cannon balls, maybe you want to use 2 separate layers for those.
     
  3. larswik

    larswik

    Joined:
    Dec 20, 2012
    Posts:
    312
    Ahhh, thanks! Since my original idea for the game has expanded I find myself writing lots of extra code. I was trying to consolidate that now and remove some layers to make things easier to work with. Thanks for verifying it!
     
  4. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    np. Usually the docs are good.:)

    It seems you can have a layer ignore a layer, an object ignore an object, but not an object ignore a layer ;)
     
  5. larswik

    larswik

    Joined:
    Dec 20, 2012
    Posts:
    312
    Thanks. I tend to go to the Doc's first or a Google search. I am going to hit the Docs now to see if there is the reverse of the Ignore Layer so that I can reset it in the destroy function for the cannon ball. That was I can just ignore it for this one object and they set up the collision after it's destroyed.

    Thanks again!
     
  6. larswik

    larswik

    Joined:
    Dec 20, 2012
    Posts:
    312
    Found it in 5 seconds looking at the Doc's. I wasn't aware that the IgnoreCollision(5,8,false); had a 3rd argument, for setting whether the collision happens or not. That method should always require that 3rd argument. I think it makes for a better written code.
     
  7. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Sometimes, methods are written with those optional parameters.. I guess the authors thought that might be more than a 50% chance people use it for true? lol I'm not sure. :)
    Glad you found it, though.