I'm trying to find a way to start/stop a camera's autoRotation in a babylonjs scene by clicking on a button outside the scene.
var createScene = function () {
var scene = new BABYLON.Scene(engine);
scene.clearColor = new BABYLON.Color3(0, 0, 0);
var camera = new BABYLON.ArcRotateCamera("Camera", Math.PI / 2, Math.PI / 2, 2, new BABYLON.Vector3(0,0,200), scene);
camera.attachControl(canvas, true);
var light1 = new BABYLON.HemisphericLight("light1", new BABYLON.Vector3(1, 1, 100), scene);
var light2 = new BABYLON.PointLight("light2", new BABYLON.Vector3(0, 1, -1), scene);
light1.intensity = 10;
light2.intensity = 24;
BABYLON.SceneLoader.ImportMesh("", "samples/79115-0_10MB/", "79115-0_100.obj", scene, function (newMeshes) {
camera.target = newMeshes[0];
});
////// set the behaviour here /////
camera.useAutoRotationBehavior = false;
return scene;
};
I have tried, unsuccessfully, a toggle function outside the createScene function to make the change:
function toggleRotate(){
if(autoR==0){
autoR=1;
camera.useAutoRotationBehavior = true;
}else{
autoR=0;
camera.useAutoRotationBehavior = false;
}
}
Okay the solution for what you are trying to do is a bit tricky.
Actually you need to bind your camera rotating to the engine rendering loop.
For example you can have the following render loop :
var scene = new BABYLON.Scene(engine);
engine.runRenderLoop(() => {
scene.render();
rotateCamera();
});
And then your rotateCamera methods contains the following instructions :
rotateCamera() {
if(autoR==1){
camera.alpha = (camera.alpha % (2*Math.PI)) + (offset);
}
}
The offset variable is the value of how much you want to rotate your camera (in radian) for each frame.
Your button then must just call a function that toggle the value of autoR
For further info don't hesitate to go to the BabylonJS Forum. https://forum.babylonjs.com/c/questions