So i’m working on a arena game where you can shoot rockets and lay mines, when one of these explode it creates a explosion prefab with the particle system and a object called and tagged explosionRangeCheck. The enemy then finds a object with that tag and checks distance to the object and if the enemy is within a certain distance it takes damage and becomes immune against explosions so it wont take more than one hit from the explosion. (I used collisions to begin with but i could not get it as accurate i wanted it to be) But this makes the enemy immune to all other explosions too which is not what i want.
Here is the code;
(2.6 being time before explosion object disapear)
var Explosion : GameObject;
var explosionImmune : float;
var minExplosionDist : float;
var explosionDistance : float;
function Update()
{
if(explosionImmune > 0)
{
explosionImmune -= Time.deltaTime;
}
Explosion = GameObject.FindWithTag("explosionRangeCheck");
if(Explosion == null)
{
explosionDistance = 0.0;
}
if(Explosion != null)
{
explosionDistance = Vector3.Distance(Explosion.transform.position, transform.position);
}
if (Explosion != null explosionDistance < minExplosionDist explosionImmune <= 0)
{
explosionImmune = 2.6;
EnemyScript.Enemy.Health -= playerShooting.ExplosionDamage;
}
}
What i want is so an enemy only can loose health one time per explosion the enemy comes in range of.
If anyone got any suggestions how i could do this would be cool.
I was thinking of maybe it would work if every explosion got put into an array that checks distance to every explosion inside to see if it’s in range of an explosion.
I dont really know what to do and i’m really tired so i’m just checking if someone got any suggestions.
//Randomly