so heres a doozy.
I implemented the particle system and decided to try and use two different effects, one for when the enemy is hit and it still has health, and one for when it is hit and has no health. To do this I used a boolean which I then set in the explosioneffects script.
however it seems that ive missed something, as when the hit take method plays, it automatically destroys the object even if the object’s health is set to more than 1. I’ve included my scripts below. Is anyone able to double check for me please? I’m sure i’ve done it right, but if ive missed something id like to know!
Explode Effects Script
{
[SerializeField] ParticleSystem ShieldedHit;
[SerializeField] ParticleSystem DeathHit;
public void PlayHitEffect(bool killer)
{
if (!killer)
{
ParticleSystem instance = Instantiate(ShieldedHit, transform.position, Quaternion.identity);
Destroy(instance.gameObject, instance.main.duration+instance.main.startLifetime.constantMax);
}
else
{
ParticleSystem instance = Instantiate(DeathHit, transform.position, Quaternion.identity);
Destroy(instance.gameObject, instance.main.duration+instance.main.startLifetime.constantMax);
}
}
}
Health script
{
[SerializeField] int health = 2;
[SerializeField] ExplodeEffects effects;
private void Start()
{
effects = GetComponent<ExplodeEffects>();
}
public void HurtUnit(int damageTaken)
{
if (!IsDead())
{
effects.PlayHitEffect(false);
health -= damageTaken;
Debug.Log($"Oww {gameObject.name} took {damageTaken} damage!");
if (IsDead())
{
health = 0;
effects.PlayHitEffect(true);
KillUnit();
}
else
{
}
}
else
effects.PlayHitEffect(false);
KillUnit();
}
public bool IsDead()
{
return health <= 0;
}
public void KillUnit()
{
GameObject.Destroy(gameObject);
Debug.Log("Oww I Am DEAD!");
}
}
Anyone have any ideas? Much appreciated!