How to control animations on models?

I am trying to make it so that when something happens (like a user presses a key) a certain part of the animation will start. But when I load the model and try to do that it does not work; the model animates in a loop. How can I stop the loop and play a certain part of the animation when the user does something. So far the code I have is:

    BABYLON.SceneLoader.ImportMesh("", " 
    assets/main/src/", "scene.gltf", scene, function(newMeshes, skeletons){
    var g = newMeshes[0];
    g.renderingGroupId = 1;
     g.parent = camera;
     g.position = new BABYLON.Vector3(1, -2, 5);
     document.body.onclick = function(){
     //the code above does not work :(
     scene.beginAnimation(skeletons[0], 0.297, 1.443, false, 0.8);


Here is a repro:
UPDATE: here is the correct repro:

The skeletons are the 3rd parameter, not the 2nd one of the ImportMesh callback.

Here’s a PG that should do what you want:

Thank you, but how do you start and stop at a certain frame or time?

start is taking a number of optional parameters:

Is the to and from key the frame?

I think you want to set the from parameter to select the starting frame.

OK thank you! I figured it out! One more thing. Some animations have a set animation name. How would I play it by that name?

I must say I don’t know the animation sub-system… Have you tried to look at the doc: Introduction to Animations | Babylon.js Documentation (and other chapters)?

If you are loading them from a gltf, all the animation groups will be in scene.animationGroups

if you know the name of your animation groups, it should be as easy as:

1 Like