I’m trying to setup an AnimationEvent to detect the end of an animation so it can go back to playing the previous one. However, i’m getting an “AnimationEvent has no reciever” error repeatedly when the event is called even tho I have added one to my script.
function Start ()
{
var event = new AnimationEvent();
event.time = animation["land"].clip.length;
event.functionName = "LandFinish";
animation["land"].clip.AddEvent(event);
}
function LandFinish()
{
print("finished the land");
}
Is there some problem with this code? Couldn’t find any examples of AnimationEvent around so not sure if my method is correct.
Also, this is my first play with animation in Unity so any tips on whether this is the best technique to use for detecting the end of an animation (ie effectively creating a onAnimationEnd event).
Maybe you should modify the LandFinish function and put this.
function LandFinish(animEvent : AnimationEvent)
{
print("finished the land");
}
I want to know the final of an animation but I have another problem. I do the same but the event isn’t triggered when the animation ends, it’s triggered more frequently.
Thanks for that info - in the end I discovered the problem was related to the location of my event in the script.
I too had the problem of the event being called multiple times so I just put a check on the time of the animation to ensure it was close to the end (the other calls occurred when the animation was near it’s start). Something like this:
function LandFinish(animEvent : AnimationEvent)
{
if (anim.time > anim.clip.length * 0.9) {
// do your stuff
}
}
Probabaly a better way of doing it and if anyone knows of a way (or why events get called when they shouldn’t) then please shout.
I know this is an old thread, but I too amy having animationEvents being called many times at once, with no clear cause. Has anyone found a solution that doesn’t involve an “if” check for every event? cpu cycles are precious on mobile, and i’d rather not dedicate them to constant if checks on animationEvents that SHOULD ONLY BE CALLED ONCE.