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

**URL:** <https://forum.babylonjs.com/t/is-it-possible-to-attach-a-different-camera-to-the-canvas-than-the-one-that-is-active-in-the-scene/14877>\
**Category:** Questions\
**Created:** [October 13, 2020, 3:20am UTC](https://forum.babylonjs.com/t/is-it-possible-to-attach-a-different-camera-to-the-canvas-than-the-one-that-is-active-in-the-scene/14877 "2020-10-13T03:20:28Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![Bikeman868](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/bikeman868/32/11616_2.png) [@Bikeman868](https://forum.babylonjs.com/u/Bikeman868)\
**Post date:** [October 13, 2020, 3:20am UTC](https://forum.babylonjs.com/t/is-it-possible-to-attach-a-different-camera-to-the-canvas-than-the-one-that-is-active-in-the-scene/14877/1 "2020-10-13T03:20:28Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![Evgeni\_Popov](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/evgeni_popov/32/4432_2.png) [@Evgeni\_Popov](https://forum.babylonjs.com/u/Evgeni_Popov)\
**Post date:** [October 13, 2020, 8:54am UTC](https://forum.babylonjs.com/t/is-it-possible-to-attach-a-different-camera-to-the-canvas-than-the-one-that-is-active-in-the-scene/14877/2 "2020-10-13T08:54:49Z")

</div>

> [@Bikeman868](#):
>
> 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?

---

<div class="post-metadata">

**Author:** ![Bikeman868](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/bikeman868/32/11616_2.png) [@Bikeman868](https://forum.babylonjs.com/u/Bikeman868)\
**Post date:** [October 13, 2020, 8:48pm UTC](https://forum.babylonjs.com/t/is-it-possible-to-attach-a-different-camera-to-the-canvas-than-the-one-that-is-active-in-the-scene/14877/5 "2020-10-13T20:48:22Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Evgeni\_Popov](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/evgeni_popov/32/4432_2.png) [@Evgeni\_Popov](https://forum.babylonjs.com/u/Evgeni_Popov)\
**Post date:** [October 13, 2020, 9:05pm UTC](https://forum.babylonjs.com/t/is-it-possible-to-attach-a-different-camera-to-the-canvas-than-the-one-that-is-active-in-the-scene/14877/6 "2020-10-13T21:05:03Z")

</div>

Check this PG:

[https://playground.babylonjs.com/#LV2H9J#1](https://playground.babylonjs.com/#LV2H9J#1)

Is it what you are looking after?

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

---

<div class="post-metadata">

**Author:** ![Bikeman868](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/bikeman868/32/11616_2.png) [@Bikeman868](https://forum.babylonjs.com/u/Bikeman868)\
**Post date:** [October 14, 2020, 5:52am UTC](https://forum.babylonjs.com/t/is-it-possible-to-attach-a-different-camera-to-the-canvas-than-the-one-that-is-active-in-the-scene/14877/7 "2020-10-14T05:52:29Z")

</div>

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](https://www.babylonjs-playground.com/#6PA720) 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.

---

<div class="post-metadata">

**Author:** ![Bikeman868](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/bikeman868/32/11616_2.png) [@Bikeman868](https://forum.babylonjs.com/u/Bikeman868)\
**Post date:** [October 14, 2020, 6:46am UTC](https://forum.babylonjs.com/t/is-it-possible-to-attach-a-different-camera-to-the-canvas-than-the-one-that-is-active-in-the-scene/14877/8 "2020-10-14T06:46:44Z")

</div>

If you comment out the viewports and the part that activates both cameras, and add a couple of buttons to [this PG](https://www.babylonjs-playground.com/#6PA720), then you can see what happens when you switch cameras.

```auto
    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";
    button1.top = '-45%';
    button1.left = '-45%';
    button1.color = "Blue";
    button1.cornerRadius = 5;
    button1.background = "White";
    gui.addControl(button1);

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

```

---

<div class="post-metadata">

**Author:** ![Evgeni\_Popov](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/evgeni_popov/32/4432_2.png) [@Evgeni\_Popov](https://forum.babylonjs.com/u/Evgeni_Popov)\
**Post date:** [October 14, 2020, 8:47am UTC](https://forum.babylonjs.com/t/is-it-possible-to-attach-a-different-camera-to-the-canvas-than-the-one-that-is-active-in-the-scene/14877/9 "2020-10-14T08:47:55Z")

</div>

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

[https://www.babylonjs-playground.com/#6PA720#247](https://www.babylonjs-playground.com/#6PA720#247)

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).

---

<div class="post-metadata">

**Author:** ![Bikeman868](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/bikeman868/32/11616_2.png) [@Bikeman868](https://forum.babylonjs.com/u/Bikeman868)\
**Post date:** [October 14, 2020, 7:15pm UTC](https://forum.babylonjs.com/t/is-it-possible-to-attach-a-different-camera-to-the-canvas-than-the-one-that-is-active-in-the-scene/14877/10 "2020-10-14T19:15:01Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![Evgeni\_Popov](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/evgeni_popov/32/4432_2.png) [@Evgeni\_Popov](https://forum.babylonjs.com/u/Evgeni_Popov)\
**Post date:** [October 14, 2020, 8:06pm UTC](https://forum.babylonjs.com/t/is-it-possible-to-attach-a-different-camera-to-the-canvas-than-the-one-that-is-active-in-the-scene/14877/11 "2020-10-14T20:06:42Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Bikeman868](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/bikeman868/32/11616_2.png) [@Bikeman868](https://forum.babylonjs.com/u/Bikeman868)\
**Post date:** [October 15, 2020, 12:19am UTC](https://forum.babylonjs.com/t/is-it-possible-to-attach-a-different-camera-to-the-canvas-than-the-one-that-is-active-in-the-scene/14877/12 "2020-10-15T00:19:26Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Bikeman868](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/bikeman868/32/11616_2.png) [@Bikeman868](https://forum.babylonjs.com/u/Bikeman868)\
**Post date:** [October 15, 2020, 6:52am UTC](https://forum.babylonjs.com/t/is-it-possible-to-attach-a-different-camera-to-the-canvas-than-the-one-that-is-active-in-the-scene/14877/13 "2020-10-15T06:52:28Z")

</div>

I modified the PG to [this version](https://www.babylonjs-playground.com/#6PA720#248) 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.

---

<div class="post-metadata">

**Author:** ![Bikeman868](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/bikeman868/32/11616_2.png) [@Bikeman868](https://forum.babylonjs.com/u/Bikeman868)\
**Post date:** [October 15, 2020, 6:59am UTC](https://forum.babylonjs.com/t/is-it-possible-to-attach-a-different-camera-to-the-canvas-than-the-one-that-is-active-in-the-scene/14877/14 "2020-10-15T06:59:58Z")

</div>

And [this version](https://www.babylonjs-playground.com/#6PA720#249) 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.

---

<div class="post-metadata">

**Author:** ![Bikeman868](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/bikeman868/32/11616_2.png) [@Bikeman868](https://forum.babylonjs.com/u/Bikeman868)\
**Post date:** [October 15, 2020, 7:14am UTC](https://forum.babylonjs.com/t/is-it-possible-to-attach-a-different-camera-to-the-canvas-than-the-one-that-is-active-in-the-scene/14877/15 "2020-10-15T07:14:30Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Deltakosh](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/deltakosh/32/26635_2.png) [@Deltakosh](https://forum.babylonjs.com/u/Deltakosh)\
**Post date:** [October 15, 2020, 3:50pm UTC](https://forum.babylonjs.com/t/is-it-possible-to-attach-a-different-camera-to-the-canvas-than-the-one-that-is-active-in-the-scene/14877/16 "2020-10-15T15:50:36Z")

</div>

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
