I have an enemy with a collider trigger. When the enemy gets hit by a bullet, it reduces his hitpoints, pretty basic.
The issue I’ve run into is that when the Enemy’s HPs are less than or equal to 0, I want to move the gameobject off the screen and disable it. All of this works great. I just created a “dead” animation when the enemy is killed, so he falls to the ground. The problem I have is that the animation plays after the object is moved and disabled, instead of before. I’ve tried different methods to fix this, but haven’t been successful. I tried using coroutines(not sure how these work exactly), but couldn’t get it working that way either. Here is the basic script I am working with:
//Attached to an enemy
function OnTriggerEnter(otherObject : Collider)
{
if(otherObject.gameObject.tag == "bullet")
{
if(EnemyHPs > 0)
{
animate.Play("Enemy_Hit");
EnemyHPs--;
}
else if(EnemyHPS <= 0)
{
animate.CrossFade("Dead");
if(!animate.IsPlaying("Dead"))
transform.position = startingPosition;
gameObject.active = false;
EnemyHPs = 3;
}
}
}
I also tried moving the code from the if statement “Enemy < 0” to the update function. I am probably way off track here, since I’m still learning how to code this type of stuff, but any help on how to get the animation to finish before the object gets disabled and moved would be greatly appreciated.
Thanks!