How to avoid camera reset when call render function [RESOLVED]

Hello guys,
this is a script

    var createScene2=function()
{
        var scene = new BABYLON.Scene(engine);
        camera = new BABYLON.ArcRotateCamera("Camera", Math.PI / 2, Math.PI / 2, 5, new 
            BABYLON.Vector3(0,0,0), scene);
            camera.attachControl(canvas, true);
           var light1 = new BABYLON.HemisphericLight("light1", new BABYLON.Vector3(1, 1, 0), 
           scene);
           var light2 = new BABYLON.PointLight("light2", new BABYLON.Vector3(0, 1, -1), scene);
           var sphere = BABYLON.MeshBuilder.CreateSphere("sphere", {}, scene);
           var MyGoal = new BABYLON.Vector3(0,5,5);
           MyCurve= MyPath(position, MyGoal);
           MoveCameraThrough(scene, camera , MyCurve);
           return scene
           }
       var scene = createScene2();
       engine.runRenderLoop(function () {
            scene.render();
    	});

        window.addEventListener("resize", function () { 
            engine.resize();
            });

Here I want to move the camera through a specific goal, so it works but when I call render function camera reset to its initial position.
I try to initialize the camera outside the createScene2() function, but it doesn’t work.

any idea ?

Anes

You are not going to be able to initialize your camera outside of the engine. I’m sure you simply wrote this as an example for reading. However, it’s difficult when you declare functions and variables that don’t exist. It is always helpful to spend 5 minutes building a playground scene. Here is a PG scene with most of your code in case you want to develop to ask further questions.

https://www.babylonjs-playground.com/#9UFEBE#43

Galen

@Galen in my case the camera dosn’t move with click mouse event.

u helped me in the last topic to move the camera through a specific path (so I just click button my camera start moving to my goal point).
problem is that when it arrive to the goal position, render will restart the animation, and the camera back to initial point and move through the goal again, and it still repeat that.

This is because your script structure appears to be such that you’re calling your initial scene values after setting your new camera values. If you build off of the Playground scene I posted, this will update all transforms inside the render loop without restarting the render loop.

Also, if you use the playground as a place to test your scene, not only will you get results quickly, but the babylon.js community will be able to update your scene with solutions and you will be able to get your scene running quickly.

Galen

@Galen I use BJS locally so I test in my browser and I call

   engine.runRenderLoop(function () {

           scene.render();
           
    	});

You will find attached all my code :

    var canvas = document.getElementById("renderCanvas"); // Get the canvas element 
    var engine = new BABYLON.Engine(canvas, true); // Generate the BABYLON 3D engine
    var MyCurve;


 var createScene2=function()
 {
        var scene = new BABYLON.Scene(engine);
        camera = new BABYLON.ArcRotateCamera("Camera", Math.PI / 2, Math.PI / 2, 5, new 
        BABYLON.Vector3(0,0,0), scene);
        camera.attachControl(canvas, true);
        var light1 = new BABYLON.HemisphericLight("light1", new BABYLON.Vector3(1, 1, 0), scene);
        var light2 = new BABYLON.PointLight("light2", new BABYLON.Vector3(0, 1, -1), scene);
        var sphere = BABYLON.MeshBuilder.CreateSphere("sphere", {}, scene);
        MyCurve= MyPath(position, MyGoal);
        MoveCameraThrough(scene, camera , MyCurve);
        return scene
    }


   var scene= createScene2();


   engine.runRenderLoop(function () {
           scene.render();        
    	});

    window.addEventListener("resize", function () { 
            engine.resize();
    });

You are setting values for your scene variable twice. This is why your scene rests to the initial values.

Galen

@Galen The first call is inside the function so it’s local and the second it is just a new var to call the function I change it but still doesn’t work, I just copied the same syntax from : First Steps - Babylon.js Documentation
and if I excute the first one template, camera updates rotations normally

Resolved the problem was the “true” in the line

scene.beginAnimation(camera, 0, 200, true);

it defines that the scene loops :slight_smile:

@Galen thank you and I wish you a speedy recovery

Anes