- Home /
OnCollisionExit or OnTriggerExit with Physics.IgnoreCollision
I must be missing how these two things work. Right now I have a border made out of objects slightly off screen so when a bouncing ball hits them it changes direction randomly. I want the ball to spawn outside and move in.
Physics.IgnoreCollision gets set to true but it's in OnCollisionEnter.
OnCollisionExit or OnTriggerExit are never called when the ball passes through, which is how I thought they were suppose to work. My assumption is that because it is now ignoring physics with that object it no longer calls either of these functions and so I can't use them to change Physics.IgnoreCollision to false.
My question is two fold:
Is that true?
If so, what else can I do?
Answer by MoMughal · Oct 26, 2018 at 02:03 PM
I also got this issue and one neat way to resolve it is to do in Coroutine like the following
private IEnumerator WaitForIgnoreCollision(GameObject a, GameObject b) { Physics.IgnoreCollision(a.collider, b.collider, true); yield return new WaitForSeconds(5f); Physics.IgnoreCollision(a.collider, b.collider, false); }
and then call it in the OnCollisionEnter at whatever condition you might need it at.
Answer by joedartjr · Jan 20, 2014 at 01:58 AM
Triggers and Colliders are one-way; like mesh textures. If you are on the "inside" of one, then the collision will not hit. There aren't enough details about where your colliders are at to know if this is the case.
Also, if the ball is a Rigidbody and you are trying to use a Trigger, then the ball has to be set to Kinematic for the trigger to take effect; conversely, if you are using colliders, then the collider cannot be labeled as a Trigger.
Hopefully something from this can point you in the right direction. If not, more details never hurt anyone, or a screenshot of how your border is setup.
I know it's not the prettiest border, I'm not too concerned about cleaning it up just yet. The ball spawns anywhere outside and goes through it but then OnTriggerExit is never called even when the ball is set to kinematic (it is using a rigidbody).
Am I missing something about triggers? Are they specific objects or can they just be any object with a collider?
Forgot the image.
If you have the ball set to $$anonymous$$inematic, then you will need to set the border objects to "Is Trigger"; just know that while the "collision" between a kinematic rigidbody and a trigger happens, you get no visible collision. Do you have a Debug.Log attached to the OnTriggerExit?
If your OnCollisionEnter is hitting properly, and causing the Physics.IgnoreCollision to properly activate then you are preventing any further collisions from happening by disabling them.
The easiest solution is to increase the size of one of your borders so that the ball will start inside of it, then you shouldn't need to worry about changing the Physics.IgnoreCollison flag at all, since the collision only happens one-way. Simply leave it enabled, and start the ball from inside the collision zone; this should allow the ball to come out of the border, but not re-enter it.
Another solution is to have the IgnoreCollision flag to be reset after a set amount of time.
public float enableCollision = 5f;
void Update
{
if (Physics.IgnoreCollision == true)
{
enableCollision -= Time.deltaTime;
}
if (enableCollision <= 0)
{
Physics.IgnoreCollision = false;
}
}
Something like that.
That's a really good idea actually. I was about to try it. Removed the code that caused the physics collision ignore, rebuilt, ran it. Still going through...
Closed unity, restarted. Rebuilt. Still going through. I have looked over my code and I have no idea why it's still doing that. This is very frustrating.
It turns out I had kinematic on. That works for the most part but the border it spawns in it goes back through for some reason.