- Home /
How do I make Collider to get through another Collider? (w/o Trigger)
I'm making 2D game, and I've made Character, bouncy Spikeball (it's spike but it bounces), Ground, and they have Collider2D. I've managed to make Character lose health when it collides with Spikeball. But I want to make Spikeball to go through Character when it collides. This is the behaviour when you set Collider to Trigger, but I can't just set Collider to Trigger because Spikeball and Character must collide with Ground. And I can't just make them doesn't collide with Project Settings>Physics2D>Layer Collision Matrix because I need to detect when Spikeball and Character collides (to make Character lose health). What should I do?
Summary
Spikeball should go through Character without colliding (like Trigger)
Spikeball should collide with anything except Character
Character should collide with anything except Spikeball
Somehow you should be able to detect when Spikeball and Character collides
Answer by Cornelis-de-Jager · Aug 01, 2019 at 05:45 AM
Simply tell the colliders to ignore eachother. It will still pickup on events I believe, but will ignore each other otherwise.
void OnCollisionEnter2D(Collision2D col)
{
if (col.transform.tag.Equals ("SpikeBall"))
Physics2D.IgnoreCollision(col, GetComponent<Collider2D>());
}
How do I detect Collision with spikeball and character after that? OnCollisionEnter2D won't activate after Physics2D.IgnoreCollision
The correct thing to do will be to have two colliders on a spikeball. One that is just a collider and one that is a trigger. That will enable you to not to collide with collider, but still shoot trigger events i believe:
void OnCollisionEnter2D(Collision2D col) =>
if (col.transform.tag.Equals ("SpikeBall"))
Physics2D.IgnoreCollision(col, GetComponent<Collider2D>());
void OnTriggerEnter2D (Collider2D col) =>
if (col.transform.tag.Equals ("SpikeBall"))
DoDamageToPlayer();
void DoDamageToPlayer() {
// Write your damage code here
}