Alternate Solution to Collision Issues

I had already fixed the collision issues with enemies blowing themselves up, but I used tags instead of layers. I know you can have a ton more tags than layers, but I don’t know if there are any issues with going this route. If anyone is curious, this is what I did.

On the enemy script:

private void OnTriggerEnter2D(Collider2D other)
{
    if (other.tag == "PlayerLaser")
    {
        DamageManager damageManager = other.gameObject.GetComponent<DamageManager>();
        if (!damageManager) { return; } //if damageManager is null, does not progress.
        ProcessHit(damageManager);
    }
}

for the player, I just changed the tag to EnemyLaser.

As for making sure the lasers disappear, I just added an else if to the laser scripts, PlayerLaser script shown below:

private void OnTriggerEnter2D(Collider2D collision)
{
    if (collision.tag == "TrashCollector")
    {
        Destroy(gameObject);
    }
    else if (collision.tag == "Enemy")
    {
        Destroy(gameObject);
    }
}

Again, for the the enemy’s laser, I just changed the tag on the EnemyLaser script (copy of the PlayerLaser script) the tag to “Player”.

I took the same approach as you before watching this lesson. I was actually expecting them to use the tags to solves this problem, so I figured why not get the job on it. My assumptions were wrong, but this method does work and works well.

Privacy & Terms