How does IgnoreCollision work? [SOLVED]

I’m spawning bullets inside the player.

The bullets are rigidbodies, and therefor I need to ignore the collision with the player.

This is exactly what the documentation says that Physics.IgnoreCollision is good for. I try and use it exactly like in the documentation:

Instantiate(pistolBullet, player.transform.position, player.transform.rotation);
                Physics.IgnoreCollision(pistolBullet.collider, collider);

But when I do this, I get an error when I instantiate a bullet saying “Ignore collision failed. Both colliders need to be activated when calling this IgnoreCollision”.

I tried putting the code right after I instantiate, like shown here. But I also tried putting it in the awake of the bullet. But that didn’t help.

I’m not sure how it works, so I’d be greatly appreciative if I could get some help.

Thank you.

Both your bullet prefab and the player do have a box collider attached, correct?

 Physics.IgnoreCollision(pistolBullet.collider, collider);

In addition you might want to change that line to…

Physics.IgnoreCollision(pistolBullet.collider, player.collider);

Just so it follows the same convention in the line above. Not critical but good form so you don’t have to think about it next time you read the code.

Hey.
Thanks, I got some help and figured it out.

My problem was that I couldn’t use a prefab, I had to use the instantiated object. So I used the code:

                GameObject bullet = (GameObject) Instantiate(pistolBullet, player.transform.position, player.transform.rotation);
                Physics.IgnoreCollision(bullet.collider, collider);

But to answer your question. Yes, they use box colliders. :slight_smile: And yeah, I agree that the second line might follow the convention better. Thanks.