Using layers for collision

IS this really a good idea?

I’ve used onCollisionEnter method to detect collision with enemies (I check the layer it’s on to know that it is an enemy I have touched).

Using layers for everything really does put me off for some reason…

1 Like

I’m interested in this way. With this lecture I noticed wall jumping is re-enabled. If I unchecked the Capsule Collider 2D Is Trigger box, the player death still triggers and now wall jumps are no longer allowed. Something about having it set as a trigger allows it to go through other colliders.

So if you use an onCollisionEnter all the time (checking for the correct collider I assume) would that have prevented wall jumps and still trigger the Dying state while keeping Is Trigger on? I guess I’m not sure why we need to set Is Trigger on the collider if it works without it checked.

I’ve done it this way and it seems cleaner (and more efficient) than calling all of these functions which may or may not do anything during Update().

    private void OnCollisionEnter2D(Collision2D collision)
    {
        // see if we are colliding with the enemy
        if (collision.gameObject.GetComponent<Enemy>() != null)
        {
             // do the stuff
        }
    }

Of course this implies that you have to still check that the collision is with the right thing in some way - I’m doing it with checking the class of the script component. Perhaps this means that I’ll eventually need an inheritance hierarchy for all of the possible enemy types with a parent type for all of them.

Privacy & Terms