Define camera / vector3 position by a variable

Hi
I tried to set a camera position by an variable, but didn’t find a solution in the doc. Is it possible to use a array variable for the vector3 position? like this it does not work:

var cameraposition1 = [’-800’,‘1000’,‘2700’];
camera.setPosition(new BABYLON.Vector3(Array(cameraposition1)));

thanks!

I’m not very familiar with *.Vector3(Array(an-array-here)), so I’m not sure if that does not work because of the ‘Array’ thing, or because of the strings instead of numbers in your array. However, this should work:

var cameraposition1 = [800, 1000, 2700]; // Numbers, not strings
camera.setPosition(new BABYLON.Vector3(cameraposition1[0], cameraposition[1], cameraposition[2]);
1 Like

thanks for the help!

the console says:

viewer.js:273 Uncaught ReferenceError: cameraposition is not defined

code:

var cameraposition1 = [800, 1000, 2700];

          /// OVERVIEW Button   

                $("#btn2").click(function () {

                  alert((cameraposition1[0]));
        
                // POSITION
                camera.setPosition(new BABYLON.Vector3(cameraposition1[0], cameraposition[1], cameraposition[2]));

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

                // SWITCH CAMERA     
                scene.activeCamera = camera;

                });

sadly it does not seem to work this way…

Hi @ueli,

Check the line where you are defining the camera position, as you are using two different variables:
cameraposition1 and cameraposition. You have only defined cameraposition1 and that’s why the console is throwing that error, since cameraposition is not defined and you have used it to try to give a position to the camera.

omg i’m blind! thanks! it works now :slight_smile:

https://doc.babylonjs.com/api/classes/babylon.vector3#fromarray

A little less writing

camera.setPosition(new BABYLON.Vector3.FromArray(cameraposition1));
2 Likes

nice! thank you