I have a coin object that will get destroyed if a player hits it. But now, I want to play an animation (sprite animation) first for that coin before getting destroyed.
What function do I need to use to trigger the destroy right after the animation finished?
thank you
look up “unity animation events”, they allow you to trigger functions at points in the animation.
A quick and dirty way would be just to start a coroutine and let it yield for the amount of seconds your animation playtime is long.
var delay = 0.5f;
IEnumerator Wait(float delay)
{
//Delay a half second before self destroy
yield return new WaitForSeconds(delay);
Destroy(Object);
}
edit
This is even better and cleaner: Unity - Scripting API: AnimationClip.length
@garrido86 if you are using the newer Animator system as opposed to the legacy animation system you’d need to get a reference to the current playing animation via Unity - Scripting API: Animator.GetCurrentAnimatorClipInfo in order for that to work with animation clip length