- Home /
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?
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
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;
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.
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?
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.
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)
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.