I have a character model with several animations, some of which I need to use to trigger certain events, such as a death animation triggering deactivation of the game object.
The problem is that intermittently the animationevent I have attached to the end of my death function will not fire until the animation plays 2 or 3 times, and sometimes it will trigger after the first play, which is what I want it to do consistently.
Here is the declaration for the animation event:
void Start()
{
AnimationEvent DeathAnimationEndedEvent = new AnimationEvent();
DeathAnimationEndedEvent.functionName = “DeathAnimationEnded”;
DeathAnimationEndedEvent.time = animation[“death1”].length;
animation[“death1”].clip.AddEvent(DeathAnimationEndedEvent);
}
And here is the function the event should be calling:
void DeathAnimationEnded()
{
Debug.Log(“death anim ended”);
// Logic to deactivate the game object goes here
}
You are probably using the wrap mode “once” (or default). After finishing the animation, it jumps to the beginning, so it probably doesn’t reach the very end every time. Try using the wrap mode “clamp forever”, it should help.
No problem, glad to help
A bit about the wrap mode “once”. I’m not sure how Unity handles the animations and the events, but imagine this situation: animation length is 2.0 seconds; in the current frame the animation time is 1.9; the next delta time is 0.2 (well, it’s not very realistic, but it doesn’t matter ), so the next frame will have the animation at 0.0 again (well, it can’t display the animation of time 2.1 when you’re in the “once” mode, it needs to jump back to the beginning). Therefore, the time 2.0 is not reached (and maybe that’s why the event isn’t triggered).
Again, I’m not sure, how Unity handles the events, but if it simply checks the time of the animation and if it’s (more or less) equal (or greater) to the event time, it triggers the event, then my example explains the problem (and that’s why “clamp forever” helps).