# Gltf Animation loops when i click, how to stop the loop

**URL:** <https://forum.babylonjs.com/t/gltf-animation-loops-when-i-click-how-to-stop-the-loop/38477>\
**Category:** Questions\
**Tags:** animation, gltf, animation-loop\
**Created:** [February 23, 2023, 3:57pm UTC](https://forum.babylonjs.com/t/gltf-animation-loops-when-i-click-how-to-stop-the-loop/38477 "2023-02-23T15:57:17Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Cie](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/cie/32/29396_2.png) [@Cie](https://forum.babylonjs.com/u/Cie)\
**Post date:** [February 23, 2023, 3:57pm UTC](https://forum.babylonjs.com/t/gltf-animation-loops-when-i-click-how-to-stop-the-loop/38477/1 "2023-02-23T15:57:17Z")

</div>

Hallo everyone,  
i made a Gltf Model with a Animation. What I want is: the animation doesn’t play at first. The animation starts playing once when I click on the model. Then I click on the model again and the animation plays backwards once. Click again to play the animation normally once. and so on. I made it BUT, when the animation is not finished, I click on the model and the animation loops automatically. I dont want it to loop. How to fix it?

I created a test model. This is my code:

> **[Babylon.js Playground](https://playground.babylonjs.com/#XCIAPF%235)**
>
> Babylon.js playground is a live editor for Babylon.js WebGL 3D scenes

var createScene = function() {  
var scene = new BABYLON.Scene(engine);

```
var camera = new BABYLON.FreeCamera("camera1", new BABYLON.Vector3(0, 5, -10), scene);

camera.setTarget(BABYLON.Vector3.Zero());

camera.attachControl(canvas, true);
BABYLON.SceneLoader.ImportMesh(
    "",
    "https://raw.githubusercontent.com/CieOriKing/TestCube/d85a6e2505c76954d8f3e338aefc04ec033cf53e/TestCube.gltf",
    "",
    scene,
    function (newMeshes) {          
        scene.createDefaultCameraOrLight(true, true, true);
        scene.createDefaultEnvironment();
        
        var animation = scene.animationGroups[0];
        animation.speedRatio = 0;
        animation.loopAnimation = false;
        var isForward = true; // 
        var isPlaying = false; // 
        var isEnd = false; // 
        var actionManager = new BABYLON.ActionManager(scene);
        newMeshes.forEach(function(mesh) {
            if (mesh.getTotalVertices() > 0) {
                mesh.actionManager = actionManager;
                mesh.actionManager.registerAction(
                    new BABYLON.ExecuteCodeAction(
                        BABYLON.ActionManager.OnPickTrigger,
                        function() {
                            // play animation
                            if (!isPlaying) {
                                isPlaying = true;
                                isEnd = true;
                                console.log("isPlaying: "+isPlaying);
                                animation.speedRatio = 1; // set the speed
                                animation.play(true);
                            } else {
                                // reverse
                                if (isEnd) {
                                    isForward = !isForward;
                                    animation.speedRatio = isForward ? 1 : -1; // switch
                                    animation.play(false);
                                } else {
                                    // stop
                                    isPlaying = false;
                                    isEnd = false;
                                    animation.pause();
                                }
                            }
                        }
                    )
                );
            }
        });
});
return scene;

```

};

---

<div class="post-metadata">

**Author:** ![sebavan](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/sebavan/32/57_2.png) [@sebavan](https://forum.babylonjs.com/u/sebavan)\
**Post date:** [February 23, 2023, 9:52pm UTC](https://forum.babylonjs.com/t/gltf-animation-loops-when-i-click-how-to-stop-the-loop/38477/2 "2023-02-23T21:52:33Z")

</div>

Looks like a bug to me, let me fix it ASAP.

---

<div class="post-metadata">

**Author:** ![sebavan](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/sebavan/32/57_2.png) [@sebavan](https://forum.babylonjs.com/u/sebavan)\
**Post date:** [February 23, 2023, 9:52pm UTC](https://forum.babylonjs.com/t/gltf-animation-loops-when-i-click-how-to-stop-the-loop/38477/3 "2023-02-23T21:52:40Z")

</div>

Assigned @sebavan on February 23, 2023, 9:52pm UTC



---

<div class="post-metadata">

**Author:** ![sebavan](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/sebavan/32/57_2.png) [@sebavan](https://forum.babylonjs.com/u/sebavan)\
**Post date:** [February 24, 2023, 2:30am UTC](https://forum.babylonjs.com/t/gltf-animation-loops-when-i-click-how-to-stop-the-loop/38477/4 "2023-02-24T02:30:24Z")

</div>

It looks like we are not supporting dynamically changing the speedratio from positive to negative. You can only do it if stopping the animation.

@Evgeni_Popov can you confirm ?

---

<div class="post-metadata">

**Author:** ![Cie](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/cie/32/29396_2.png) [@Cie](https://forum.babylonjs.com/u/Cie)\
**Post date:** [February 24, 2023, 10:45am UTC](https://forum.babylonjs.com/t/gltf-animation-loops-when-i-click-how-to-stop-the-loop/38477/5 "2023-02-24T10:45:26Z")

</div>

Thanks for your answers. Is it possible to disable mouse clicks while the animation is running?

---

<div class="post-metadata">

**Author:** ![Evgeni\_Popov](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/evgeni_popov/32/4432_2.png) [@Evgeni\_Popov](https://forum.babylonjs.com/u/Evgeni_Popov)\
**Post date:** [February 24, 2023, 12:42pm UTC](https://forum.babylonjs.com/t/gltf-animation-loops-when-i-click-how-to-stop-the-loop/38477/6 "2023-02-24T12:42:06Z")

</div>

I found a way to make it work:

> **[Babylon.js Playground](https://playground.babylonjs.com/#XCIAPF%238)**
>
> Babylon.js playground is a live editor for Babylon.js WebGL 3D scenes

Just stopping the animation and setting the speed ratio was not working (at least for me), the animation was restarting from frame 0 and not from the current frame. I needed to get the current frame and play the animation from this frame to the `from` or `to` frame value, depending on the direction of the animation.

@Cie You can check `animation.isStarted` to know if the animation is still playing or not to disable mouse clicks.

---

<div class="post-metadata">

**Author:** ![sebavan](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/sebavan/32/57_2.png) [@sebavan](https://forum.babylonjs.com/u/sebavan)\
**Post date:** [February 24, 2023, 1:54pm UTC](https://forum.babylonjs.com/t/gltf-animation-loops-when-i-click-how-to-stop-the-loop/38477/7 "2023-02-24T13:54:22Z")

</div>

Unassigned @sebavan on February 24, 2023, 1:54pm UTC



---

<div class="post-metadata">

**Author:** ![Cie](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/cie/32/29396_2.png) [@Cie](https://forum.babylonjs.com/u/Cie)\
**Post date:** [February 24, 2023, 2:34pm UTC](https://forum.babylonjs.com/t/gltf-animation-loops-when-i-click-how-to-stop-the-loop/38477/8 "2023-02-24T14:34:39Z")

</div>

Thank you very much!

---

<div class="post-metadata">

**Author:** ![Evgeni\_Popov](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/evgeni_popov/32/4432_2.png) [@Evgeni\_Popov](https://forum.babylonjs.com/u/Evgeni_Popov)\
**Post date:** [January 25, 2024, 4:01pm UTC](https://forum.babylonjs.com/t/gltf-animation-loops-when-i-click-how-to-stop-the-loop/38477/9 "2024-01-25T16:01:48Z")

</div>

This PR will make your first PG work, without the start/stop workaround:

> <https://github.com/BabylonJS/Babylon.js/pull/14740>
>
> See https://forum.babylonjs.com/t/animationgroup-play-backwards-issue/47502/5 an…d https://forum.babylonjs.com/t/gltf-animation-loops-when-i-click-how-to-stop-the-loop/38477.
