If there are issues when only enabling the FreeCamera

I have a Babylon.js playground link: https://playground.babylonjs.com/#CTCSWQ#1405. I’m trying to implement a switch between first and third person view.

When I only enable line 40 and comment out line 41, everything works fine and the camera switch works as expected. However, when I enable line 41 and comment out line 40, the FreeCamera becomes inactive.

I suspect that this issue occurs because cameras not in the activeCameras list are frozen. Can someone confirm this? And if so, how should I handle this situation?

Any help or suggestions would be greatly appreciated. Thank you!

Let me add @PolygonalSun our camera Guru

1 Like

So, I’m not quite sure that I understand what you mean by a switch but based on the code that I’m seeing, it looks like you’re trying to go for a split-screen look. In any case, if there are cameras in the activeCameras, only those cameras will be updated. Now, if I’m misunderstanding things, please let me know.

If you’re going for a toggle between the two cameras, you’ll have to find a way to update the parent camera when the child camera is active. Here’s an idea that came to mind:

// First, you should comment out any lines that add active cameras
// When the first person camera is active, it will change it's view matrix and this will be called
// When inactive, this won't be called
firstPersonCamera.onViewMatrixChangedObservable.add((eventData) => {
                thirdPersonCamera.update();
    });

// This is just a simple swap when you press 'C'
    scene.onKeyboardObservable.add((eventData) => {
        if (eventData.event.code === "KeyC") {
            if (scene.activeCamera === thirdPersonCamera) {
                scene.activeCamera = firstPersonCamera;
            }
            else {
                scene.activeCamera = thirdPersonCamera;
            }
        }
    }, BABYLON.KeyboardEventTypes.KEYUP);

Keep in mind that there are many ways to do this and this is just one way (possibly not even the most optimal way). I’m not sure if this is what you’re going for but maybe this will work for you.

2 Likes

That’s what I wanted. Thank you