Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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
0
Question by SteenPetersen · Feb 19, 2019 at 12:08 PM · collisiondetectignoreignorecollision

How to detect if collisions are being ignored unity 2018

In unity 2019 Unity exposed Physics.GetIngoreCollision();

How would one go about doing this in unity 2018 in a clean way?

Comment
Add comment
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

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by Bunny83 · Feb 19, 2019 at 12:31 PM

You don't. There was no way to query this information in the past. However since you have to explicitly call Physics.IgnoreCollision to ignore / unignore collisions between two colliders you just have to track this yourself. If you want to handle this in a general manner you can use a struct with two Collider references as key into a dictionary. Keep in mind that the when you swap the two colliders it would be a different key, so you might want to either add both versions to the dictionary or when checking the state, try both versions. Which solution is better depends on what operation has to be done more often. Ignoring / unignoring collisions or querying the state

Comment
Add comment · Show 4 · 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 SteenPetersen · Feb 19, 2019 at 01:13 PM 0
Share

I have decided against this method because I think its beco$$anonymous$$g too convoluted and I'd rather organise the code a bit. Also not very used to using structs so I just wanted to ask you if this would be a way of doing what you said:

     struct CollisionsIgnored {
 
         Dictionary<Collider, Collider> collisions;
 
         public bool isIgnored(Collider a, Collider b)
         {
             if (collisions.Contains$$anonymous$$ey(a))
             {
                 return true;
             }
             else if (collisions.Contains$$anonymous$$ey(b))
             {
                 return true;
             }
 
             return false;
         }
 
         public void AddCollisionIgnore(Collider a, Collider b)
         {
             if (!collisions.Contains$$anonymous$$ey(a))
             {
                 collisions.Add(a, b);
             }
         }
 
         public void RemoveCollisionIgnore(Collider a, Collider b)
         {
             if (collisions.Contains$$anonymous$$ey(a))
             {
                 collisions.Remove(a);
             }
         } 

     }
 
     CollisionsIgnored ci;
avatar image Bunny83 SteenPetersen · Feb 19, 2019 at 09:57 PM 1
Share

No ^^. I said using a struct as key. Something like this:

 public struct ColliderPair
 {
     public Collider col1;
     public Collider col2;
     public ColliderPair(Collider aCol1, Collider aCol2)
     {
         col1 = aCol1;
         col2 = aCol2;
     }
 }
 
 private static Dictionary<ColliderPair, bool> m_IgnoreCollisions = new Dictionary<ColliderPair, bool>();
 
 public static void IgnoreCollision(Collider a, Collider b, bool ignore = true)
 {
     Physics.IgnoreCollision(a, b, ignore);
     if (ignore)
         m_IgnoreCollisions[new CollisionPair(a, b)] = true;
     else
     {
         m_IgnoreCollisions.Remove(new CollisionPair(a, b));
         m_IgnoreCollisions.Remove(new CollisionPair(b, a));
     }
 }
 
 public static bool GetIgnoreCollision(Collider a, Collider b)
 {
     if (m_IgnoreCollisions.Contains$$anonymous$$ey(new CollisionPair(a, b)))
         return true;
     if (m_IgnoreCollisions.Contains$$anonymous$$ey(new CollisionPair(b, a)))
         return true;
     return false;
 }

You can't use a single collider as key since one collider could have received multiple ignore calls with different colliders. The dictionary should track each pair of colliders. Ins$$anonymous$$d of a Dictionary you could use a HashSet since we only want to track if an ignore collision has been set for a pair or not.


This implementation uses the lease memory as each pair is only added once. When checking we have to test both cases. We could simply add both to the dictionary which simplifies the check to a single case. However this doubles the size of the dictionary.

avatar image xxmariofer Bunny83 · Feb 19, 2019 at 10:17 PM 0
Share

he only wants to get the collision of a sword with the body parts. isnt just easier to have a list of colliders that are currently collision-inactive inside a sword script? this method would work really good for tracking multiple colliders colliding all with each other, but if its only one (the weapon) collider with the parts he can just have a list with those colliders inside the weapon script, or i am missing something?

Show more comments
avatar image
1

Answer by xxmariofer · Feb 19, 2019 at 12:27 PM

cant you use layers? rather than ignoring collision between 2 objects set the 2 objects in the desired layer and ignore those collision you can use this method. if you cant use layers for some reason, i would create a manger for saving in a dictionary/list the objects that have been set to ignore collision but i would rather go with the first approach for better performance.

Comment
Add comment · Show 2 · 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 SteenPetersen · Feb 19, 2019 at 12:30 PM 0
Share

Thanks for the reply, I wont be using layers for this because it becomes a mess, I'm making blades penetrate into colliders and it's working fine as is, parts of the blade sticking out of the collider should still collide with objects in the same layers etc. So I need to ignore only a specific collider, not an entire layer.

Thanks for the suggestions but yes a dictionary would be a bit cumbersome for this, Im wondering what the code looks like on unitys end for solving this.

--edit (Spelling)

avatar image xxmariofer SteenPetersen · Feb 19, 2019 at 12:45 PM 0
Share

then having a list of objects with all the objects inside a blade script should do it, if you only need to add/check if contais the colliders.

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

154 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 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 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

Ignore individual collisions between two objects 1 Answer

Ignoring collision on a specific parent or a tagged object 1 Answer

making Navmesh Agents able to walk through each other? 3 Answers

Why does my player object decide to ignore colliders whenever he feels like it? c# 1 Answer

Instantiated Projectile IgnoreCollision Error 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