Unity 2D RPG: Complete Combat System - Enemy Projectiles Passing Through Player

Hello there! I seem to have run into a snag! I have a capsule collider at the feet of my player so that they don’t run through objects, and a boxcollider2D with “Set Trigger” enabled to work as a hitbox.

The problem is this: the projectiles will hit the player and trigger the TakeDamage() Function, but only if they hit the capsule collider at the feet of the player.

How do I address the projectiles passing through the boxcollider2D is trigger component?

HitboxProblem

I’m guessing this stems from the code snippet:

private void OnTriggerEnter2D(Collider2D other) 
    {
        EnemyHealth enemyHealth = other.gameObject.GetComponent<EnemyHealth>();
        Indestructible indestructible = other.gameObject.GetComponent<Indestructible>();
        PlayerHealth player = other.gameObject.GetComponent<PlayerHealth>();

        if (!other.isTrigger && (enemyHealth || indestructible || player))
        {

            if (player && isEnemyProjectile)
            {
                player.TakeDamage(1, transform);
            }
            Instantiate(particleOnHitPrefabVFX, transform.position, transform.rotation);
            Destroy(gameObject);
        }

    }

That pesky little part:

!other.isTrigger

Appears to be the source of the problem, but if this isn’t included, projectiles will hit tree transparency detection colliders and the canopy layer.

I think the easiest solution would be a second if check which looks for isTrigger and player

private void OnTriggerEnter2D(Collider2D other) 
    {
        EnemyHealth enemyHealth = other.gameObject.GetComponent<EnemyHealth>();
        Indestructible indestructible = other.gameObject.GetComponent<Indestructible>();
        PlayerHealth player = other.gameObject.GetComponent<PlayerHealth>();

        if (other.isTrigger && player)
        {
            if (isEnemyProjectile)
            {
                player.TakeDamage(1, transform);
            }
            Instantiate(particleOnHitPrefabVFX, transform.position, transform.rotation);
            Destroy(gameObject);
        }

        if (!other.isTrigger && (enemyHealth || indestructible || player))
        {
            if (player && isEnemyProjectile)
            {
                player.TakeDamage(1, transform);
            }
            Instantiate(particleOnHitPrefabVFX, transform.position, transform.rotation);
            Destroy(gameObject);
        }

    }
1 Like

The course just uses the collider at the player’s feet, which fits with the idea of the character being “in front of” shots that are behind the player.

That being said, I believe @Satinel’s solution should make the 2nd collider work properly.

1 Like

Thanks everyone!

Once the ghost was firing the shots at the player’s transform, despite the fact that the player’s hitbox was relatively small, the enemies still landed their shots consistently, and it still felt right.

After playtesting a little bit, I think the box collider 2d is just too big, and by the time I shrink it to a size that feels right, it’s roughly the same size as the player’s original capsule collider, so I think I may just stick with the original capsule collider. Though, maybe I’ll try adding a larger hitbox to enemies later via this logic to make player aiming a little bit easier!

Thanks for helping me test the option of a hitbox, @Satinel ! I got a little stuck there without your help!

2 Likes

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms