Bullet OverlapShere Explosion only dealing damage to enemies and damageable game objects sometimes.

Getting headaches from this problem, I created a simple weapon shooting script which would instantiate bullets to the pointed direction with a certain amount of force.

This bullet when it collides with certain tags it will instantiate an overlap sphere at it’s position, dealing damage to all game objects with colliders it touches. And it works, but only sometimes.

Relevant projectile code:

private void OnCollisionEnter(Collision collision)
{
    //Don't count collisions with other bullets
    if (collision.collider.CompareTag("Projectile")) return;

    //Count up collisions
    collisions++;

    //Explode if bullet hits an enemy directly and explodeOnTouch is activated
    if (collision.collider.CompareTag("Enemy") && explodeOnTouch) Explode();
    if (collision.collider.CompareTag("DamageableNature") && explodeOnTouch) Explode();
}

private void Explode()
{
    //Instantiate explosion
    if (explosion != null) explosion = Instantiate(explosion, transform.position, Quaternion.identity);
    
    //Check for enemies 
    Collider[] enemies = Physics.OverlapSphere(transform.position, explosionRange, whatIsEnemies);
    foreach (Collider collider in enemies)
    {
        //Get component of enemy and DamageableNature and call Take Damage 

        //Just an example!
        if (collider.CompareTag("Enemy"))
        {
            collider.GetComponent<EnemyScript>().TakeDamage(damage);
        }
        else if (collider.CompareTag("DamageableNature"))
        {
            collider.GetComponent<DamageableNature>().TakeDamage(damage);
        }
    }
}

Relevant Enemy code:

public int maxHealth;
[HideInInspector]
public int health;

void Start()
{
    health = maxHealth;
}

public void TakeDamage(int damage)
{
    health -= damage;

    if (health <= 0)
    {
        Die();
    }
}

private void Die()
{
    Destroy(gameObject);
    Debug.Log("GameObject Destroyed");
}

If I have to guess this issue is most likely from the Colliders of the GameObject, but after messing around with it nothing seemed to change. The shot at GameObject will only sometimes get destroyed from certain shots.

Note: for example a damageable wall, it’s Box Collider is slightly bigger than the wall itself.

I would really appreciate some help or feedback on fixing this issue, or even making the code listed cleaner if needed.

Thank you for the explanation.

IGNORE, THIS IS A SPAM BOT

Awesome and interesting article. Great things you’ve always shared with us. Thanks. Just continue composing this kind of post. (SPAM LINK REMOVED)

I figured it out, for anyone wondering how to achieve this issue, for the explode function just add a parameter of a Vector3, and use that position to point where the OverlapShere will be created just like this:

private void Explode(Vector3 explosionPoint)

Collider enemies = Physics.OverlapSphere(explosionPoint, explosionRange, whatIsEnemies);
_

Then with OnCollsionEnter call the explode function and fill in the parameter with this simple line of code:

collision.contacts[0].point
_

the end result should look like this:

private void OnCollisionEnter(Collision collision)
{
        //Explode if bullet hits an enemy directly and explodeOnTouch is activated
        if (collision.collider.CompareTag("Enemy") && explodeOnTouch) Explode(collision.contacts[0].point);
}

private void Explode(Vector3 explosionPoint)
{
    //Check for enemies 
    Collider[] enemies = Physics.OverlapSphere(explosionPoint, explosionRange, whatIsEnemies);
    foreach (Collider collider in enemies)
    {
        //Get component of enemy and call Take Damage
        if (collider.CompareTag("Enemy"))
        {
            collider.GetComponent<EnemyScript>().TakeDamage(damage);
        }
    }
}