2d explosion damage
Hi, i am trying to make a grenade that explodes and gives damages to the Enemys. Spent hours trying but got nowhere
The explosion funcation void Explode() {
Collider[] colliders = Physics.OverlapSphere(transform.position, radius);
foreach(Collider nearbyObject in colliders)
{
Enemy rb = nearbyObject.GetComponent<Enemy>();
if(rb != null)
{
//rb.addExplosionForce(Force, transform.position, radius);
enemy.DamageEnemy(damage);
}
}
Debug.LogError("Script 20021312");
// Remove missile
Destroy(gameObject);
}
The Enemy's health function public void DamageEnemy(int damage) {
stats.curHealth -= damage;
if (stats.curHealth <= 0)
{
GameMaster.KillEnemy(this);
// Spawn Blood
//Instantiate(particles, firePoint.position.y);
obamaparticle();
Debug.LogError("Sending...");
}
if (statusIndicator != null)
{
statusIndicator.SetHealth(stats.curHealth, stats.maxHealth);
}
}
I would appreciate any help thanks
Comment
Answer by KoenigX3 · Jun 03, 2020 at 02:50 PM
When you check the colliders, you are not accessing the actual enemy scripts. It's sort of confusing. You have an Enemy class rb, and then you have an enemy object, which is unknown to us, since i don't see where it came from.
foreach(Collider nearbyObject in colliders)
{
Enemy enemyScript = nearbyObject.gameObject.GetComponent<Enemy>();
if(enemyScript != null) enemyScript.DamageEnemy(damage);
Rigidbody rb = nearbyObject.gameObject.GetComponent<Rigidbody>();
if(rb != null) rb.AddExplosionForce(force);
}