As the subject says,is there a built in function that could be automatically called once an animation stops playing?
I’d like to call a function once and not check on Update() via clip.isPlaying. For ex-Director users, I’m looking for something like registerScript(#bonesPlayer).
Thanks!
1 Like
I’ve been asking the same question. I’d like to know the answer. Perhaps there is no such function and that is why no one has answered our posts yet.
1 Like
I found this but have yet to work on an efficient method and if it works if the speed of the clip is adjusted - in my opinion this would be better than checking through Update().
You can use animation.IsPlaying to determine whether a named animation has finished.
Yes, but…
How?
Tho’ I’ve found IsPlaying() and isPlaying…
… but what is the best way to apply this?
This:
function SomeFunction () {
animation.wrapMode = WrapMode.Once;
animation.CrossFade("myAnimation");
While (isPlaying) {
yield;
}
DoSomething();
}
… just seems wrong.
Tho’ in my case, I think I can use the example in:
function SomeFunction () {
animation.wrapMode = WrapMode.Once;
animation.CrossFade("myAnimation");
yield WaitForSeconds (animation.clip.length);
DoSomething();
}
[edit]
Tho’ oddly enough, I found (at least when using EZ GUI), I couldn’t get that to work unless I removed the co-routine (eg: yield) into another function:
function SomeFunction () {
animation.wrapMode = WrapMode.Once;
animation.CrossFade("myAnimation");
WaitForSomething ();
}
function WaitForSomething () {
yield WaitForSeconds (animation.clip.length);
DoSomething();
}
Not sure why…
[/edit]
{
animation.clip=anim_Walk;
animation.Play();
//–
AnimationEvent animEv= new AnimationEvent();
animEv.functionName=“animFinished”;
Debug.Log(“anim length=”+anim_Walk.length);
animEv.time=anim_Walk.length;
animEv.time=1.0f;
anim_Walk.AddEvent(animEv);
}
void animFinished()
{
Debug.Log(“@@@@@anim finished@@@@@”);
}
Hi there, I think it would work…
public AGoodClassName : Monobehaviour // Just for idea, do not copy it. Just take the code
{
public Animation Clip walkClip; // Drag & drop the anim you want to play in the inspector tab
void Update()
{
animation.clip = walkClip; // Write the name of the animation you wrote above
StartCoroutine(“WaitForAnimToPlay”);
}
IEnumerator WaitForAnimToPlay()
{
animation.Play();
yield return new WaitForSeconds (animation.clip.length);
Debug.Log (“Animation has played once”);
}
// Type in whatever you want to do after the animation clip has played. I wrote this code keeping in mind that if i wanted to create sparks particle effect from gun of my enemy every time he has fired once, I’ll do it this way (that’s why I started the coroutine in Update method). If you want to play your animation in the start just type this code in Start method. Also, if you do it in Start method you can just click on the animation in Assets and check the length of the animation and use this: if (Time.TimeSinceLevelLoad > = the length of your clip) { Do whatever you want to}
but I think there should be something like if (animation.hasPlayed) {Do blah and blah}
Hope it helps.