Hi, I am trying to make the object fade out when its killed, however all it does is just disappear when the destroy is called. Please can someone figure out where I have gone wrong trying to lerp the alpha channel, I thought I had done this correctly!
I cant see the wood for the trees anymore
void Dead()
{
if (audio.isPlaying) return;
audio.PlayOneShot(enemyDeathSounds[Random.Range(0, enemyDeathSounds.Length)]);
enemyAlive = false;
anim.enabled = false;
movement.enabled = false;
hearing.SetActive(false);
sight.SetActive(false);
melee.SetActive(false);
nav.enabled = false;
StartCoroutine(DestroyCorpse());
}
IEnumerator DestroyCorpse()
{
yield return new WaitForSeconds(corpseFadeDelay);
StartCoroutine(FadeTo(1.0f, 2.0f));
Destroy(this.gameObject);
}
IEnumerator FadeTo(float fadeValue, float aTime)
{
float alpha = model.renderer.material.color.a;
for(float t = 0.0f; t < 1.0f; t +=Time.deltaTime / aTime)
{
Color newColor = new Color(1, 1, 1, Mathf.Lerp(alpha,fadeValue, t));
model.renderer.material.color = newColor;
yield return null;
}
}
Any help would be greatly appreciated Oh and if my code is badly written, bear with me I am still learning!