OnPickTrigger only works once. Why?

Could you help me understand why this code only works once?

I got the mesh to spin and change colour. But this only works once (unless I refresh the page/press play again). I would like it to spin and change colour every time it gets clicked on.

playground

var createScene = function () {

    // This creates a basic Babylon Scene object (non-mesh)
    var scene = new BABYLON.Scene(engine);

    // This creates and positions an arc rotate camera (non-mesh)
    var camera = new BABYLON.ArcRotateCamera("Camera", BABYLON.Tools.ToRadians(90), BABYLON.Tools.ToRadians(90), 5, new BABYLON.Vector3(0, 0, 0), scene);
    camera.attachControl(canvas, false);

    // This creates a light, aiming 0,1,0 - to the sky (non-mesh)
    var light = new BABYLON.HemisphericLight("light", new BABYLON.Vector3(0, 1, 0), scene);

    // Default intensity is 1. Let's dim the light a small amount
    // light.intensity = 0.7;

    //Creates a new standard material and sets the diffuse color to blue.
    var newMaterial = new BABYLON.StandardMaterial;
    newMaterial.name = "newMaterial";
    newMaterial.diffuseColor = new BABYLON.Color3(0.81, 0.14, 0.14);

    // Let's look at how we can load custome assets.
    // var sphere = BABYLON.MeshBuilder.CreateSphere("sphere", {diameter: 2, segments: 32}, scene);
    // sphere.material = newMaterial;


    // var sphere = BABYLON.SceneLoader.ImportMesh("", "https://raw.githubusercontent.com/BabylonJS/MeshesLibrary/master/", "shaderBall.glb", scene);
    // sphere.material = newMaterial;

    BABYLON.SceneLoader.ImportMesh("", "https://raw.githubusercontent.com/BabylonJS/MeshesLibrary/master/", "shaderBall.glb", scene, function(newMeshes){

        var sphere = newMeshes[0].getChildMeshes()[0];

        sphere.material = newMaterial;

        sphere.actionManager = new BABYLON.ActionManager(scene);

        sphere.actionManager.registerAction(
            new BABYLON.InterpolateValueAction(
                BABYLON.ActionManager.OnPickTrigger,
                light,
                'diffuse',
                BABYLON.Color3.Random(),
                1000
            )
        );

        sphere.actionManager.registerAction(
            new BABYLON.InterpolateValueAction(
                BABYLON.ActionManager.OnPickTrigger,
                sphere,
                'rotation',
                new BABYLON.Vector3(Math.PI*10, Math.PI*10, Math.PI*10),
                1000
            )
        );

    });

    return scene;

};

Hi @treakec, it’s because the value of the trigger stays the same, so there is nothing new to interpolate between. You can change it like this:

https://playground.babylonjs.com/#27KY6R#1

2 Likes