- Home /
Explosion damage script only deals damage in the first explosion and none afterwards
I have a script that takes all gameobjects in the scene, and if they have a damage detector and is within the radius of the explosion damage, deals damage to them. This works fine for just the first explosion I deal near the enemy, but not anything else.. Note the first explosion is far away from the player, so the player should not be affected by it.... Every explosion has this script on it.
foreach (GameObject item in EntitiesDataBase.GameObjects) {
if (Vector3.Distance(item.transform.position, transform.position) < ExplosionDistance) {
if (item != null) {
if (item.GetComponent<Rigidbody>() != null) {
item.GetComponent<Rigidbody>().AddExplosionForce(ExplosionForce, transform.position, ExplosionDistance); // Just incase this is stopping the script with an error.....
}
if (item.GetComponent<DamageDetector>() != null) {
float Damage = 0;
float Distance = Vector3.Distance(transform.position, item.transform.position);
// Make an equation between distance and damage
Vector2 Pos1 = new Vector2(0, MaxDamage);
Vector2 Pos2 = new Vector2(ExplosionDistance, 0);
float SlopeN = Pos1.y - Pos2.y; // Numerator
float SlopeD = Pos1.x - Pos2.x; // Denominator
float YIntercept = MaxDamage;
float Slope = SlopeN / SlopeD;
Damage = Distance * Slope + YIntercept;
item.GetComponent<DamageDetector>().Health += -1 * Damage;
}
}
}
}
"Damage Detector" is a universal script I use for detecting damage for other scripts to use. EntitesDataBase.GameObjects
is an array that when a gameobject not in the array already is found, it adds it (so each gameobject has a unique ID number, results in some/most gameobjects being null)
The values that I have it set to, incase that has anything to do with it, is the following ExplosionDistance = 20; MaxDamage = 100;
hard to say. i would suggest applying some general debugging practices:
put print statements in to confirm that the code is executing the way you think.
is the explosion script even being called ?
how many items are in your GameObjects collection ?
what's the distance from the explosion to the items ?
are the explosion and items where you expect ?
do the items have those components ?
note, if it would be an error for the items to not have those components, you should have an
else {}
clause which prints an error. that's just good program$$anonymous$$g practice.
unrelated, your code seems odd because line 3 checks if item
is null, but line 2 assumes it is not null.
also, your math to compute damage seems a bit more complex than it needs to be. I'd suggest looking into InverseLerp() and Lerp(). eg,
float normalizedDistance = InverseLerp($$anonymous$$Dist, maxDist, actualDist);
float damage = Lerp(maxDamage, $$anonymous$$Damage, normalizedDistance);
@elenzil That unrelated portion is what was going on... Swapped them both and it works perfectly . I guess it was the first enemy that once destroyed, it was stopping all the other explosion script attempts as they got to the null object before it got to the objects it needed to.
I'll take a look into Lerping and InverseLerping to try and simplify my code! THAN$$anonymous$$S!
Your answer
Follow this Question
Related Questions
Problem with calculating damage, value returning 0 1 Answer
How to you make a number squared in C#? 2 Answers
Distribute terrain in zones 3 Answers
Damage sending with grenades. 1 Answer
Multiple Cars not working 1 Answer