Is it possible to attach a different camera to the canvas than the one that is active in the scene?

I started from the “walking and looking around” PG in which there is a first person camera representing your character’s position, that is controlled by the input devices, and another 3rd person camera that is behind and above your character so that you can see your character in the viewport. The first person camera is the parent of the 3rd person camera so that they move together, and the 3rd party view is always of the back of your character.

I want to allow the player to switch cameras, but activating the 3rd person camera makes the input devices move this camera relative to the character instead of moving the character, which is obviously not desirable. I also have an aerial view camera that the player can choose as their view of the game.

I tried detaching the 3rd person camera from the canvas, when it is activated in the scene, but then the input devices just stop working.

Is there any way to have the scene viewport display camera A, but the device inputs move camera B?

Doesn’t attaching camera B and not A to the canvas work?

No, it looks as if only the active camera receives input.
I am going to try writing a custom camera input and see if I can make that work.

Check this PG:

Is it what you are looking after?

You can comment one of the attachControl if you don’t want the camera to move.

This PG is not what I want at all. Let me try again to explain.

The player of my game has a character and the character can walk through the scene. I want to give the player the option of either looking through the eyes of the character (first person) or view the scene from behind and above their character (third person).

I started from this PG Babylon.js Playground which has both first person and third party views but in separate viewports. It does this using two cameras where the third person camera is a child of the first person camera so it can be positioned relative to the character.

My problem is that I want to switch the active camera to give the player the choice of first person or third person view, but when I activate a camera in the scene, the input controls affect the position of that camera, not the position of the player’s character in the scene.

I hope that explains it more clearly. Thank you for trying to help.

If you comment out the viewports and the part that activates both cameras, and add a couple of buttons to this PG, then you can see what happens when you switch cameras.

    const gui = BABYLON.GUI.AdvancedDynamicTexture.CreateFullscreenUI("gui");

    const button1 = BABYLON.GUI.Button.CreateSimpleButton('button1', '1');
    button1.onPointerUpObservable.add(function(){ scene.activeCamera = camera; });
    button1.width = "50px"
    button1.height = "40px"; = '-45%';
    button1.left = '-45%';
    button1.color = "Blue";
    button1.cornerRadius = 5;
    button1.background = "White";

    const button2 = BABYLON.GUI.Button.CreateSimpleButton('button2', '2');
    button2.onPointerUpObservable.add(function(){ scene.activeCamera = viewCamera; });
    button2.width = "50px"
    button2.height = "40px"; = '-40%';
    button2.left = '-45%';
    button2.color = "Blue";
    button2.cornerRadius = 5;
    button2.background = "White";

You have to detach the previous camera and attach the new one:

If you want your character to also move when you switch to the viewCamera I think you will have to parent it to this camera at switch time (and re-parent it to camera when you switch back).

Hmmm, the character mesh is also parented to the camera, just like in the PG I started from. This makes the character move through the scene. If I switch the character meshes to be children of the other camera, I will also have to change their position and direction to compensate for the cameras being in different locations. There is also the complication that the third person camera targets (points towards) the character mesh, and I am not sure how this will work if the camera is the mesh’s parent.

More fiddling around in the PG needed I think.

If there is a way to have a camera handle inputs and move through the scene even when it is not the active camera for the scene, this would solve my problem, and I could simply switch cameras.

By the way, I also have an aerial view, so that’s a third camera to have to deal with!

In that case why not attaching the controls to both cameras? They will both move when you use the inputs.

In this case the inputs move the third person camera, but it is a child of the first person camera and it’s relative position is supposed to be fixed, i.e. it is supposed to be a fixed distance away from the player character.
Maybe if I moved the third person camera outside of the player character I could make it work, but then I would have to do complicated math to keep the third person camera looking at the back of the player as the player turns etc.

I modified the PG to this version which is slightly better. With this version the first person camera works as we want, and when I switch to the third person camera the right/left buttons correctly rotate the first person camera, and the up/down buttons do modify the cameras direction, and the speed is set correctly, but the camera does not move forward.

Even if I manually change the camera position to try to move it, it still does not move. The camera must know that it is not active in the scene, and locks down the position of the camera.

And this version fixes the problem with using the mouse to look around, which now works properly for both cameras.

Just for fun I am going to add the aerial camera too.

I am coming to the conclusion that the only way to fix this is to write a custom camera. I think it is not achievable with the UniversalCamera which seems odd to me, because I don’t think I am doing anything very extraordinary. I have played many games where you can switch between camera views.

Well this is probably a simple engineering decision in your code. It is really easy to have multiple cameras and switch between them with no problem (and you can easily attach/detach control),
Your decision of having the character as a child of the camera is what limits you.
That being said it is just as easy to call mesh.setParent(null) and then mesh.setParent(newCamera) to literally switch the parent with no consequence

1 Like