Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 TerraPass · Oct 31, 2013 at 02:34 AM · collisionlayersignore collisions

Ignore collisions between a certain collider and a layer

Is there a way to ignore collisions between a single collider and a layer?

I've found Physics.IgnoreCollision() and Physics.IgnoreLayerCollision() methods, however the first one only allows to ignore collision between individual objects, while the latter only works for entire layers.

Thanks.

Comment
Add comment · Show 3
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 LijuDeveloper · Oct 31, 2013 at 04:05 AM 1
Share
  1. $$anonymous$$ake two layers , one layer for one collider and other layer other object

  2. For example "Player" layer for player , "IgnoreCollisionLayer" for which layer want to ignore

  3. Set layer for player and other collider

  4. Go to Edit > Project Settings > Physics

  5. In Physics manager you can see Layer Collision $$anonymous$$atrix ( Triangle shape checkboxs)

  6. Disable collision between "Player" and "IgnoreCollisionLayer"

avatar image TerraPass LijuDeveloper · Oct 31, 2013 at 02:21 PM 0
Share

Thanks for your answer. Yes, I thought about that too. However the problem with this approach is that it does not allow to dynamically (i.e. at runtime) designate certain single collider to ignore collisions against a layer. I guess one could programatically assign an object to a layer, but this solution is not always applicable (what if there are several collider-layer pairs that need to be excluded from collision detection?).

avatar image Tatsiga · Feb 26, 2014 at 11:02 PM 0
Share

Im new to all this unity develop thing and this was a really big help, so thanks :)

2 Replies

· Add your reply
  • Sort: 
avatar image
2

Answer by Max_Bol · Jun 24, 2015 at 01:18 AM

There's an API that allow you to dynamically turn off the collision between colliders from 2 different layers :

Physics.IgnoreLayerCollision

That is how you change the collisions in the Physic menu (inspector) through scripts.

EDIT - Sorry didn't seen you have seen it already. But there's a possible fix.

You could do a small work-around. You duplicate the single's collider's GameObject and place it as the child of the original GameObject. On the parent, you keep the renderer component (to show what it looks like) and on the child (the copy), you remove the renderer and only apply a Mesh Collider (or whatever collider you wish to uses). As you might have already guessed, you could keep the child (collider) in a different layer than its parent. Then you can uses the IgnoreLayerCollision to only affect the collider (child) while keeping the original (renderer + mesh) in its original layer.

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
0

Answer by hjc_ · May 20, 2020 at 08:28 PM

Old question, but sharing my solution just in case.

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!

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

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

19 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

Related Questions

Layer Collision issue 1 Answer

How to go about having two seperate Game Objects follow eachother? 1 Answer

Button to change layers 1 Answer

Script that activates all objects in layer on collision not working 2 Answers

Unity Netwroking Layer Masks? 0 Answers


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