I’m in the process of making a sidescrolling shooter sort of game in a fantasy setting. One of my player’s abilities is to conjure a flame wall anywhere on the terrain. When an enemy walks across flame wall particle system’s prefab’s box collider, it sets the bool isOnFire == true in that enemy’s script. The enemy script also inherits flameWallDuration, flameWallTimer, flameWallDelay and flameWallDamage from another script. All floats. flameWallDuration is set to 2, both flameWallTimer and flameWallDelay are set to 0.5f, and flameWallDamage is set to 20. This particular enemy’s health, monsterOneHealth, is equal to 30. The following is the relevant script:
using UnityEngine;
using System.Collections;
public class MonsterTwoScript : AttackScript
{
public GameObject monsterTwo;
private float monsterTwoHealth = 30;
private bool isOnFire = false;
void Start ()
{
this.flameWallDuration = 2;
this.flameWallTimer = 0.5f;
}
// Update is called once per frame
void Update ()
{
if(monsterTwoHealth <= 0)
{
Destroy(gameObject);
}
Destroy(gameObject, 8);
if(isOnFire == true)
{
this.flameWallDuration -= Time.deltaTime;
if(this.flameWallDuration != 0)
{
this.flameWallTimer -= Time.deltaTime;
if(this.flameWallTimer < 0)
this.flameWallTimer = 0;
if(this.flameWallTimer == 0)
{
this.monsterTwoHealth -= (flameWallDamage/4);
this.flameWallTimer += flameWallDelay;
}
}
if(this.flameWallDuration < 0)
this.flameWallDuration = 0;
if(this.flameWallDuration == 0)
this.isOnFire = false;
if(this.transform.childCount > 0 && isOnFire != false)
{
GameObject fireEffect = this.transform.GetChild(0).gameObject;
fireEffect.particleEmitter.emit = true;
}
}
}
void OnTriggerEnter (Collider other)
{
if(other.gameObject.tag == "FlameWall")
{
isOnFire = true;
}
}
}
The problem is, whenever this enemy catches on fire, it will either not take any damage at all or it will continue to take damage well past the end of flame wall’s 2 second duration, and it will eventually kill the enemy, which shouldn’t happen since it should only take flameWallDamage/4 (5 damage) a total of 4 times (20 damage), and the enemy has 30 health.
I’m new to programming, and I’ve learned most of what I know by trial and error, but I can’t seem to figure this out. Throwing a bunch of “this” tags onto it was my latest effort to get things to work right, but I still cannot figure this out.
Could someone please help me out here?