Unable to update ArcRotateCamera position

Hey there!

In my project, I have an ArcRotateCamera in my scene and, at some point in time, I would like to move the camera around programmatically. However, changing camera.position.x, for example, doesn’t seem to be working.

Here is a PG where I have set up a similar scenario. I am trying to change the position of the camera inside registerBeforeRender, but it seems like it is not working. https://www.babylonjs-playground.com/#12WBC#1248

Any ideas?

Thanks in advance!
Ilde.

1 Like

Seems that you need alpha or radius (or both) parameters of ArcRotate camera - https://www.babylonjs-playground.com/#12WBC#1250

Yeah, updating the alpha works. But I’d like to update the position vector directly. Is that possible?

Yep it’s possible! You have to set the position vector entirely. I changed your demo to be:

 var x = 0;
    scene.registerBeforeRender(() => {
        camera.position = new BABYLON.Vector3(x++,0,0);
    });

https://www.babylonjs-playground.com/#12WBC#1254

Also for a more in depth example I suggest you take a look at this demo. I like it a lot cause it shows panning with the mouse and then updating the position of the camera based on that:
https://playground.babylonjs.com/#5QBZT0#9

Take a look at line 160 where it updates the target instead.
camera.target.addInPlace(panVec);

2 Likes

It is possible - https://www.babylonjs-playground.com/#12WBC#1253
Still I wouldn’t recommend to use this solution in the render loop - there will be too many Vector3 objects.

6 Likes

Hello!
You have to recalculate the angles after you change the position or the target of an ArcRotateCamera.

        camera.rebuildAnglesAndRadius()

https://www.babylonjs-playground.com/#12WBC#1255

5 Likes

@roland Your solution is the best one :slight_smile:

2 Likes