IgnoreCollision problem

I’m busy doing the Walker Boys 2D Mario clone tutorial, and I’m having an issue trying to get my gumbas to ignore each other. Each gumba is tagged as “enemy”, and each has 2 collision boxes (tagged as “enemyCollisionLeft” and “enemyCollisionRight”). I want the gumbas to react to collisions with the Mario character and various blocks, but to ignore each other. This is what I’ve tried:

 void OnTriggerEnter(Collider other)
 {
        if (other.tag == "enemy" || other.tag=="enemyCollisionLeft" || other.tag=="enemyCollisionRight")
       {                          
             Physics.IgnoreCollision(collider, other.collider);                     
       }
}

This does NOT work… and the gumbas keep getting stuck on each other whenever they collide. Any suggestions on how to get this working?

Oh, I don’t know if it makes a difference, but the gumbas do not have rigidbodies, but are using a charcter controller. Might this be the issue?

For colliders you use OnCollisionEnter instead of OnTriggerEnter. That should fix your problem.

– Ted de Munnik

Ted, OnCollisionEnter requires at least one of the objects to have a rigidbody, and the Collision parameter doesn’t have any info about what object is “providing” the collision.

First of all, OnCollisionEnter does work with 2 colliders, as far as i am concerned.

Also, OnCollisionEnter does get a Collision parameter, but did you check what is inside of that parameter?

You can see there, you can access the transform and gameObject of the object provided the collision.

If you want objects to collide, you should not use isTrigger.

I’ve tried using OnCollisionEnter (and thank you for that link, very helpful!) … it never gets called.

The problem, from what I’ve been able to tell, is that in my original code the IgnoreCollision only seems to work on the collision boxes themselves (which are set as triggers). It does not disable collisions for the CharcterController… and the gumbas therefore keep colliding. At least, thats what I think is happening. I can sidestep the issue by never allowing the gumbas to follow the same patrol paths, but this seems a silly way of fixing it.

As an aside… the objects are colliding just fine using isTrigger, I’ve used it successfully with all object interactions so far (pickups, damage, pathing etc etc). The only issue I’m having is when I don’t want the collision to happen. Is there something “wrong” with using triggers instead of OnCollision?

OK, solved a different way. I placed the gumbas on a seperate layer, and used the code below in the gumba script to make sure no collisions occured.

void Start()
{
    Physics.IgnoreLayerCollision(gameObject.layer,gameObject.layer);
}

Hi,

I don’t want to revive old threads but maybe it is useful to people who is doing the Mario 2D tutorial by Walker Boys.

Like Nomad72 said:

So I solved it changing the part of:

Physics.IgnoreCollision(other.collider, collider); //not working

with:

Physics.IgnoreCollision(other.GetComponent(CharacterController), controller); //it works!

Hope it helps someone.

Hi, i have the same issue.

Another way to resolve if you don’t want to change the code, is to move down the “Box Collider” component below the “Character Controller” component (in the Gumba Object).

If you do this, it should work, at least for me it worked.