Callback when AnimStateGraph is done playing the current animation

Is there a way to get a callback when the AnimStateGraph is done playing the animation of the current state?

I could think of using Animation Events and adding them to the animations in my state graph at runtime, but I’m not sure how to do that. Maybe there is some easier solution.

On the Animation asset, add an event with a name and the time when it should fire in the event:

In code, listen for the event via the anim component:

    this.entity.anim.on('end-animation', function() {
        console.log('hello');
    }, this);

Sorry maybe i should have specified i want to do that at runtime. As i have quite a bunch of animations and i don’t want to manually do this for every animation.

You can add the events at runtime on the anim asset resource:

    const walkTrack = assets.walkAnim.resource.animations[0].resource;
    // Add two anim events to the walk animation, one for each foot step. These events should occur just as each foot touches the ground
    walkTrack.events = new pc.AnimEvents([
        {
            time: walkTrack.duration * 0.1,
            name: 'foot_step',
            bone: 'R_foot0002_bind_JNT'
        },
        {
            time: walkTrack.duration * 0.6,
            name: 'foot_step',
            bone: 'L_foot0002_bind_JNT'
        }
    ]);

Example here: https://playcanvas.github.io/#/animation/events

1 Like

In the example the animation asset is passed into the function.

Is there a way to get the animation asset resource from the AnimComponent?

I found this.entity.anim.animationAssets but that only contains the asset id. Is there no direct reference?

If you have the assetId, you can get the asset via the assetRegistry#get

eg

this.app.assets.get(someAssetId);
1 Like

Thanks got it working!

1 Like

Bear/Bare in mind that animationAssets is not public API and there isn’t a public API to get the assigned assets to the component so that may change in the feature.

Yeah almost nothing is public API in the territory that I’m in ^^

1 Like