Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
2
Question by Tezemi · May 11, 2017 at 03:31 AM · physics2dlayersignorecollisionignore collisionslayercollision

Layer Collision Matrix, Don't Ignore Triggers

I'm currently working on a 2D game, and I was wondering if there is anyway I can use the Layer Collision Matrix to disable collisions between layers, but not ignore triggers? alt text

Right now, I need the Player layer and the Item layer to ignore collisions, that way the player doesn't push items around when he touches them before he picks them up. However, I need the player to be able to pick up items, so it would be excellent if I could disable collisions, but not the triggers.

Comment
Add comment · Show 5
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image RobAnthem · May 11, 2017 at 03:37 AM 0
Share

why not just make the Item be a trigger ins$$anonymous$$d of collision?

avatar image Tezemi RobAnthem · May 11, 2017 at 04:07 AM 0
Share

the item needs to collide with other gameobjects

avatar image ritoban Tezemi · May 11, 2017 at 04:09 AM 0
Share

Collide as in being pushed around by?

Show more comments
Show more comments

3 Replies

· Add your reply
  • Sort: 
avatar image
3

Answer by Geo3 · Feb 03, 2019 at 09:16 PM

You can use a child gameobject with it's own collider set to a different layer with it's own interaction rules/ layer mask.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
1

Answer by ritoban · May 11, 2017 at 02:04 PM

You can just set the items' colliders "Is Trigger" value to true (and leave the collision matrix checked). This means that the OnTriggerEnter/OnTriggerEnter2D functions will be called, but the items will not react to Physics (being bumped into by other colliders)

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
1

Answer by Tezemi · May 11, 2017 at 04:55 PM

Here's a solution I've come up with. It checks each collider in the scene, disables all layer collision ignores, and then enables collision ignores between colliders that are not triggers, so you can use the layer collision matrix to ignore collisions between game objects, but keep the triggers still enabled. It makes use of a custom tuple class, because tuples are not supported in .NET 3.5, which you can find here: https://gist.github.com/destinhebner/76f668b2b7705f9890ba8dec13503ab8

 public static void UpdateCollisionLayerMatrix()
 {
     List<Tuple<Collider2D, Collider2D>> collidersToUpdate = new List<Tuple<Collider2D, Collider2D>>();
     Collider2D[] colliders = FindObjectsOfType(typeof(Collider2D)) as Collider2D[];
     if (colliders == null) return;
 
     foreach (Collider2D colliderA in colliders)
     {
         foreach (Collider2D colliderB in colliders)
         {
             if (colliderA.gameObject == colliderB.gameObject) continue;
                     
             if (Physics2D.GetIgnoreLayerCollision(colliderA.gameObject.layer, colliderB.gameObject.layer))
             {
                 collidersToUpdate.Add(new Tuple<Collider2D, Collider2D>(colliderA, colliderB));
             }
         }
     }
 
     foreach (Tuple<Collider2D, Collider2D> tuple in collidersToUpdate)
     {
         Physics2D.IgnoreLayerCollision(tuple.ItemA.gameObject.layer, tuple.ItemB.gameObject.layer, false);
         if (!tuple.ItemA.isTrigger && !tuple.ItemB.isTrigger)
         {
             Physics2D.IgnoreCollision(tuple.ItemA, tuple.ItemB, true);
         }
     }
 }

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image RobAnthem · May 11, 2017 at 04:57 PM 0
Share

Hah I saw this answer and was going to immediately say... You know tuples aren't supported right? But then I saw this custom tuple class and it kind of made me giddy +1

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

69 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

IgnoreLayerCollision not disabling collisions 1 Answer

Problem with colliders in topdown 2d scene with many floors 1 Answer

IgnoreCollision with Particle System 1 Answer

Collision functions without physical collisions 1 Answer

Collision Matrix not working as intended 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges