I need help with my explosion

I found a nice prefab on the Unity store of a nuclear explosion. I’ve got it all working, kinda.

They player can die when it explodes too close, but the enemies I have setup do not take any damage. They take damage from everything else I have, but not this explosion so I think it might be something with the explosion code.

Is anyone able to help me with this?

Any help is very much appreciated.

Here’s my explosion code:

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

public class nukeExplosion : MonoBehaviour
{
    [SerializeField] float duration;
    [SerializeField] int damage;
    [SerializeField] float damageRadius;
    [SerializeField] float maxDamageDistance;

    SphereCollider explosionCollider;
    float currentRadius;
    [SerializeField] float expandSpeed;
    [SerializeField] float targetRadius;

    // Start is called before the first frame update
    void Start()
    {
        explosionCollider = GetComponent<SphereCollider>();
        currentRadius = 1f;
        Destroy(gameObject, duration);
        StartCoroutine(ExpandCollider());
    }

    IEnumerator ExpandCollider()
    {
        while (currentRadius < targetRadius)
        {
            currentRadius += expandSpeed * Time.deltaTime;
            explosionCollider.radius = currentRadius;
            yield return null;
        }
    }

    // OnTrigger method to detect nearby objects and apply damage
    private void OnTriggerEnter(Collider other)
    {
        Debug.Log("Collider Stayed: " + other.name);
        IDamage dmg = other.GetComponent<IDamage>();

        if (dmg != null)
        {
            float distance = Vector3.Distance(transform.position, other.transform.position);

            float damageMultiplier = Mathf.Clamp01(1f - (distance / maxDamageDistance));

            int calculatedDamage = Mathf.RoundToInt(damage * damageMultiplier);

            dmg.TakeDamage(damage);
        }
    }

    // Draw the damage radius gizmo for visualization
    private void OnDrawGizmosSelected()
    {
        Gizmos.color = Color.red;
        Gizmos.DrawWireSphere(transform.position, damageRadius);
    }
}

Hello @Zuhan,

The nuke script checks the units entering it’s trigger and inflicts damage, this mean the nuke trigger should be able to interact with the units, so for a first lead you can check if your enemies are on a different layer that the player and if yes you can check the collision matrix to see if a collision can occur (Unity - Manual: Layer-based collision detection)
On a second step you can check what differ between the player and the enemy units and based on the collider interaction matrix, see if the collision is possible

“New users can only share 1 image per post” or something popped up. Made 2 replies.

My bad I forgot a detail, did you make sure your enemy script inherits from IDamage?

Here’s the EnemyAI script. I think it ok? Also, I appreciate the assistance.

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

public class enemyAI : MonoBehaviour, IDamage
{

    //Serialized fields for enemy ai
    [SerializeField] NavMeshAgent agent;
    [SerializeField] Renderer model;
    [SerializeField] int HP;
    [SerializeField] int pointsToGain;
    [SerializeField] GameObject bullet;
    [SerializeField] float shootRate;
    [SerializeField] Transform shootPos;
    [SerializeField] int faceTargetSpeed;
    bool playerInRange;
    Vector3 playerDir;
    bool isShooting;
    Color enemycolor;
    

    // Start is called before the first frame update
    void Start()
    {
        //updates enemy count on start instance
        gameManager.instance.updateGameGoal(1);
        //get starter enemy color
        enemycolor = model.material.color;
    }

    void Update()
    {

        if (playerInRange)
        {
            playerDir = gameManager.instance.player.transform.position - transform.position;
            agent.SetDestination(gameManager.instance.player.transform.position);

            if (!isShooting)
                StartCoroutine(Shoot());

            if (agent.remainingDistance <= agent.stoppingDistance)
            {
                faceTarget();
            }
        }
    }
    void OnTriggerEnter(Collider other)
    {

        if (other.CompareTag("Player"))
        {
            playerInRange = true;
        }
    }
    void OnTriggerExit(Collider other)
    {

        if (other.CompareTag("Player"))
        {
            playerInRange = false;
        }
    }
    void faceTarget()
    {
        Quaternion rot = Quaternion.LookRotation(playerDir);
        transform.rotation = Quaternion.Lerp(transform.rotation, rot, Time.deltaTime * faceTargetSpeed);
    }

    // Take Damage AI added by Matt
    public void TakeDamage(int damage)
    {
        HP -= damage;
        StartCoroutine(FlashRed());
        //set destination when damaged
        agent.SetDestination(gameManager.instance.player.transform.position);
        if (HP <= 0)
        {            
            //removes a enemy from enemy count
            gameManager.instance.updateGameGoal(-1);
            
            Destroy(gameObject);
            //Points manager points add... (works? Sometimes?)
            PointsManager.Instance.AddPoints(pointsToGain);
            //Game manager points add... (Works, but not connected to player script)
            gameManager.instance.pointsChange(pointsToGain);
            Debug.Log("Enemy died. Player gained " + pointsToGain + " points.");
        }
    }
    IEnumerator FlashRed()
    {
        model.material.color = Color.red;
        yield return new WaitForSeconds(0.1f);
        model.material.color = enemycolor;
    }

    IEnumerator Shoot()
    {
        isShooting = true;
        Instantiate(bullet, shootPos.position, transform.rotation);
        yield return new WaitForSeconds(shootRate);
        isShooting = false;
    }

}