
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class EnemyManager : MonoBehaviour
{
public List<Enemy> enemiesInTrigger = new List<Enemy>();
public List<ExplosiveBarrel> explosiveBarrels = new List<ExplosiveBarrel>();
private void Start()
{
enemiesInTrigger.Clear();
explosiveBarrels.Clear();
}
public void AddEnemy(Enemy enemy) => enemiesInTrigger.Add(enemy);
public void RemoveEnemy(Enemy enemy) => enemiesInTrigger.Remove(enemy);
public void AddBarrel(ExplosiveBarrel barrel) => explosiveBarrels.Add(barrel);
public void RemoveBarrel(ExplosiveBarrel barrel) => explosiveBarrels.Remove(barrel);
}
The Enemy Manager script is used to hold references to all enemies in trigger and also any barrel.
In Start it will clear out any lingering targets.
It has two functions for each to Add and Remove objects from the lists. (I do it here so the barrel can explode / disappear or do whatever and also so if the barrel were to be destroyed by something other than itself it’ll still safely be removed by being called by the thing that destroyed it instead.
using UnityEngine;
using System.Collections;
public class ExplosiveBarrel : MonoBehaviour
{
public float health;
public bool hasExploded;
Rigidbody rb;
EnemyManager enemyManager;
public float maxDamage = 4f;
public float radius = 2.5f;
public float delayOfBarrel = .4f;
public GameObject prefab;
private void Start()
{
enemyManager = FindObjectOfType<EnemyManager>();
//add this script/object to the list of barrels using our AddBarrel() function in enemyManager
//this is done with a Trigger volume on the player of our example
rb = GetComponent<Rigidbody>();
}
void Update()
{
if (health <= 0 && !hasExploded)
{
StartCoroutine(WaitAndExplode());
hasExploded = true;
}
}
public void TakeDamage(float amount) => health -= amount;
public void AddForce(Vector3 dir, float force) => rb.AddForce(dir * force, ForceMode.Impulse);
private void OnDrawGizmos() => Gizmos.DrawWireSphere(transform.position, radius);
public IEnumerator WaitAndExplode()
{
if (enemyManager.explosiveBarrels.Contains(this))
{
enemyManager.RemoveBarrel(this);
}
yield return new WaitForSeconds(delayOfBarrel);
//barrel stuff
var colliders = Physics.OverlapSphere(transform.position, radius);
if (colliders != null)
{
foreach (var col in colliders)
{
if (col.TryGetComponent(out ExplosiveBarrel barrel) && barrel != this)
{
if (barrel.hasExploded == false)
{
var otherBarrelRb = barrel.GetComponent<Rigidbody>();
otherBarrelRb.AddExplosionForce(250, transform.position, 3f);
barrel.TakeDamage(3f);
}
}
}
}
Instantiate(prefab, transform.position, Quaternion.identity);
Destroy(gameObject);
}
}
In the Barrel script the Barrel will StartCoroutine(WaitAndExplode()); when health reaches zero.
In the coroutine it will check if it’s contained in the EnemyManager’s barrel list and if is will remove itself
Then regardless if that happened it’ll still play all the effects and explode
this grabs all colliders in its radius and checks for a barrel script.. also ensuring && barrel != this) it’s not itself if it has barrel and is not itself, then first check if its not already exploding
(it may been have just damaged and already taken max damage and is exploding)
..and then we add force and only set its health damage.. it’ll determine whether to Explode or not.