OverlapSphere Problem.

Namaste Developers. _/\

My game has a theme of ‘Survival Shooter’.
Every Enemy has 2 different colliders
(Capsule Collider & Sphere Collider).
Now, when I use Physics.OverlapSphere for the explosion or making a Tornado Effect, the enemy got killed but it is counting the Enemy DeathCount twice (1 Enemy Killed = 2 Enemy Death Count & Score Value = 20). It is so because I have 2 different colliders on enemy gameObject. When I turn off Sphere Collider then it is counting the death count and Scores normally i.e., 1 enemy killed = 1 enemy deathcount and Score value = 10. My Sphere Collider is a Triggered Collider and my Tornado also has a Sphere collider which is a Triggered too. My Tornado has a script. Below is my code.

private void OnTriggerStay(Collider other)
    {
        colliders = Physics.OverlapSphere(transform.position, Radius, CollidingLayerMask, QueryTriggerInteraction.Ignore);
       
        foreach (Collider Hit in colliders)
        {
         
            Rigidbody RB = Hit.GetComponent<Rigidbody>();
            if (RB != null)
            {
                RB.AddExplosionForce(Power, transform.position, Radius, 5.0f, ForceMode.Acceleration);
            }

            if ((RB != null) && (Hit.gameObject.tag == "Enemies" || Hit.gameObject.tag == "Birds"
            || Hit.gameObject.tag == "DYZ" || Hit.gameObject.tag == "Ghost" || Hit.gameObject.tag == "RBOSS"
            || Hit.gameObject.tag == "YBOSS" || Hit.gameObject.tag == "SBOSS"))
            {
                Hit.GetComponent<NavMeshAgent>().enabled = false;
                RB.constraints &= ~RigidbodyConstraints.FreezePositionY;

                Instantiate(TornadoDeathFx, onePosition.position, onePosition.rotation);
                Instantiate(CoinPrefab, onePosition.position, CoinPrefab.transform.rotation);

                RB.AddExplosionForce(Power, transform.position, Radius, 5.0f, ForceMode.Impulse);
                EH = Hit.GetComponent<EnemyHealth>();
                EH.TakeDamageFromTornado(100);
                
            }
        }
    }

It’s been so long I am suffering from this problem. I am frustrated and I am loosing my edge.
The edge which strikes fuel for the ‘Game Development’.

Any Help will be heartily appreciated.

Ok…

Let’s step thru this in order.

We know there are two colliders per enemy. We know you are counting both colliders, so you are getting twice the score you should with your code. We need to find a way that we only count one of the two colliders on the enemies.

One solution would be to create a “death limit” in the script controlling the death of the enemy. You could have:

      foreach (Collider collider in colliders)
      {
            var enemyController = collider.GetComponent<MyScript>();
            myScript.Die();
      }

And then in whatever is controlling your Enemy Death Script:

private bool isDead;

and when you call Die(), you set the flag to true;

public void Die()
{
      if (!isDead)
      {
            // apply visual effects and count points here
            isDead = true;
      }
}

So, this will only count points on each object once no matter how many times “Die()” is called.

Another way to do this is to isolate which collider you are testing. We can do this by trying to cast the Collider to a SphereCollider. Only the SphereColliders will be successfully cast from Collider, so you can check to see if they are null, and only proceed if they are not.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class TestScript : MonoBehaviour {

    public float radius;
    public LayerMask mask;
    public QueryTriggerInteraction query;
    private int i;

    private void Start ()
    {
        TestCollider();
    }

    private void TestCollider ()
    {
        Collider[] colliders = Physics.OverlapSphere(transform.position, radius, mask, query);

        foreach (Collider collider in colliders)
        {
            SphereCollider sc = collider as SphereCollider;
            if (sc != null)
            {

                // apply visual effects and count points here

                // i++;
                // Debug.Log("Sphere: " + i);
            }
        }
    }
}

Does this help?