- Home /
Reset a collider during runtime to lose previous Physics.IgnoreCollision() calls
I need this functionality for re-using pooled projectiles.
In this post: https://forum.unity.com/threads/clear-ignored-collisions-set-in-physic-ignorecollsion.346846/#post-2243944 The poster says "Just deactivate and reactivate the collider", however setting BoxCollider.enabled to false then true again does not make the projectile forget previous Physics.IgnoreCollision() calls.
What other way is there to deactivate and reactivate the collider? Is there another way to lose previous Physics.IgnoreCollisions() calls?
Answer by Incode · Oct 30, 2019 at 11:35 PM
As I understand it, that post is suggesting that you disable the collider and enable it when you want to use it, as opposed to using Physics.Ignore collision. That is a valid option.
Alternatively just cache the two colliders you were ignoring, and call Physics.IgnoreCollision(collider1, collider2, false) to re-enable their collisions.
I wouldn't want the global disable since I only want to ignore specific collisions: "projectile with its owner" and "monster's projectile with other monsters".
I'm working on the cache right now, just hoping it isn't expensive.
Since I want to completly reset IgnoreCollisions I was hoping there would be a one-size-fits-all reset function that would be lighter than undoing all of them one-by-one.
Here's what I'm using now:
void OnCollisionEnter(Collision col)
{
// Never collide with owner
if (ReferenceEquals(col.gameObject, owner))
{
ignoredColliders.Add(col.collider);
if (colliderBox)
Physics.IgnoreCollision(col.collider, colliderBox);
else
Physics.IgnoreCollision(col.collider, colliderCapsule);
return;
}
// Ignore monster friendly fire
else if (col.collider.tag.Equals("$$anonymous$$onster") && owner.tag.Equals("$$anonymous$$onster"))
{
ignoredColliders.Add(col.collider);
if (colliderBox)
Physics.IgnoreCollision(col.collider, colliderBox);
else
Physics.IgnoreCollision(col.collider, colliderCapsule);
return;
}
}
and then:
if (colliderBox)
{
while(ignoredColliders.Count > 0)
{
Physics.IgnoreCollision(ignoredColliders[ignoredColliders.Count - 1], colliderBox, false);
ignoredColliders.RemoveAt(ignoredColliders.Count - 1);
}
}
else if(colliderCapsule)
{
while (ignoredColliders.Count > 0)
{
Physics.IgnoreCollision(ignoredColliders[ignoredColliders.Count - 1], colliderCapsule, false);
ignoredColliders.RemoveAt(ignoredColliders.Count - 1);
}
}
Seems to work well enough, I realise now I'm probably over-estimating how often I need to ignoreCollisions() so this will hopefully not slow down my game.
Your answer
Follow this Question
Related Questions
Detect if a non-trigger collider is inside another collider 1 Answer
How to check is object directly next to another object? 1 Answer
Drag and Drop Objects in Game Problem 2 Answers
Why when i move the player object through the door the ontriggerenter/exit event are not fire ? 2 Answers
What is the best way to check is object intersecting? 1 Answer