# Issue with camera attach, detach in 5.0.0

**URL:** <https://forum.babylonjs.com/t/issue-with-camera-attach-detach-in-5-0-0/28861>\
**Category:** Bugs\
**Created:** [March 29, 2022, 3:20pm UTC](https://forum.babylonjs.com/t/issue-with-camera-attach-detach-in-5-0-0/28861 "2022-03-29T15:20:21Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![satguru](https://avatars.discourse-cdn.com/v4/letter/s/e480ec/32.png) [@satguru](https://forum.babylonjs.com/u/satguru)\
**Post date:** [March 29, 2022, 3:20pm UTC](https://forum.babylonjs.com/t/issue-with-camera-attach-detach-in-5-0-0/28861/1 "2022-03-29T15:20:21Z")

</div>

In my app I have a use case where in “on pointer down” I do, “camera.detachControl()” (as I do not want the camera to interfere) and do things like drag etc.  
Then at the end, “on pointer up” I reattach control using “camera.attachControl(…)”.  
Works fine in previous versions  
In 5.0.0 after pointerup, camera remain unresponsive. So if you try rotating the camera by pressing mouse left button and dragging, it does not respond.  
But if you click once and try again it works.

see

> **[Babylon.js Playground](https://playground.babylonjs.com/#PW460Z)**
>
> Babylon.js playground is a live editor for Babylon.js WebGL 3D scenes

click once and then try to rotate camera. it will not work  
click again and try to rotate it will work

change version to an older version say 4.2  
click once and then try to rotate camera. it will work

---

<div class="post-metadata">

**Author:** ![kaliatech](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/kaliatech/32/13557_2.png) [@kaliatech](https://forum.babylonjs.com/u/kaliatech)\
**Post date:** [March 29, 2022, 3:34pm UTC](https://forum.babylonjs.com/t/issue-with-camera-attach-detach-in-5-0-0/28861/2 "2022-03-29T15:34:41Z")

</div>

I don’t know what changed, and maybe that’s important to figure out.

But if looking only for a solution, then I suspect this is due to global event handlers being problematic. I believe the [BJS team generally recommends using the observables](https://forum.babylonjs.com/t/onpointerup-and-friends-vs-onpointerobservable-current-recommendations/28816) instead. Here’s an alternative with observables that doesn’t have the problem:

- [BJS Playground - Forum 28861](https://playground.babylonjs.com/#3RZYKH)

```javascript
    scene.onPointerObservable.add(pointerInfo => {
        if (pointerInfo.type === BABYLON.PointerEventTypes.POINTERDOWN) {
            if (firstTime) {
                console.log("pointer down, detaching control");
                camera.detachControl();
            }
        }
        else if (pointerInfo.type === BABYLON.PointerEventTypes.POINTERUP) {
            if (firstTime) {
                console.log("pointer up, attaching control");
                camera.attachControl(canvas, true);
                firstTime=false;
            }            
        }
    })

```

---

<div class="post-metadata">

**Author:** ![satguru](https://avatars.discourse-cdn.com/v4/letter/s/e480ec/32.png) [@satguru](https://forum.babylonjs.com/u/satguru)\
**Post date:** [March 29, 2022, 3:53pm UTC](https://forum.babylonjs.com/t/issue-with-camera-attach-detach-in-5-0-0/28861/3 "2022-03-29T15:53:54Z")

</div>

Nice!  
That does solve my problem and I will use your solution instead.  
Thanks for the link to the discussion too.  
Though the discussion was about using “scene.onPointerUp”, instead of “window.onPointerUp”, which is what I was using, I do get the concern.

As far my issue with window.onPointerUp I will let the BJS team decide if it is worth researching.

Thanks again

---

<div class="post-metadata">

**Author:** ![satguru](https://avatars.discourse-cdn.com/v4/letter/s/e480ec/32.png) [@satguru](https://forum.babylonjs.com/u/satguru)\
**Post date:** [March 30, 2022, 2:06am UTC](https://forum.babylonjs.com/t/issue-with-camera-attach-detach-in-5-0-0/28861/4 "2022-03-30T02:06:28Z")

</div>

Spoken too soon I guess 🙂  
Couple of issues with onPointerObservable.

1. I am doing canvas.onpointerdown too and there is no canvas.onPointerObservable

2. I am also interested in pickingInfo.  
Unfortunately the pickingInfo provided by onPointerObservable only picks mesh which are enabled, visible and have isPickable set to true. Now with scene.pick() I can provide my own predicate which can handle meshes which are disabled, invisible or have isPickable set to false.  
So if I use onPointerObservable I will have to discard the pickingInfo provided by it and make a separate scene.pick() call. Which means I end up doing picking twice and thus might incur some performance cost.  
Or is there a way to set a predicate with scene.onPointerObservable ?

---

<div class="post-metadata">

**Author:** ![satguru](https://avatars.discourse-cdn.com/v4/letter/s/e480ec/32.png) [@satguru](https://forum.babylonjs.com/u/satguru)\
**Post date:** [March 30, 2022, 2:36am UTC](https://forum.babylonjs.com/t/issue-with-camera-attach-detach-in-5-0-0/28861/5 "2022-03-30T02:36:24Z")

</div>

Well it seems there is a scene.pointerDownPredicate  
So if I  
set scene.pointerDownPredicate  
and then do  
scene.onPointerObservable .add()  
then it works.  
Problem with scene.pointerDownPredicate is that, setting it will override the previous value.

Looks like lots of hoops to go thru to use onPointerObservable.  
canvas.addEventListener(“pointerdown”, …) with scene.pick() seems more and more appropriate and performant.

Now only if BJS team can fix this issue with pointerdown 🙂  
.

---

<div class="post-metadata">

**Author:** ![sebavan](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/sebavan/32/57_2.png) [@sebavan](https://forum.babylonjs.com/u/sebavan)\
**Post date:** [March 30, 2022, 11:29am UTC](https://forum.babylonjs.com/t/issue-with-camera-attach-detach-in-5-0-0/28861/6 "2022-03-30T11:29:04Z")

</div>

@PolygonalSun would be great if you could have a quick look ?

---

<div class="post-metadata">

**Author:** ![PolygonalSun](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/polygonalsun/32/2704_2.png) [@PolygonalSun](https://forum.babylonjs.com/u/PolygonalSun)\
**Post date:** [March 30, 2022, 6:49pm UTC](https://forum.babylonjs.com/t/issue-with-camera-attach-detach-in-5-0-0/28861/7 "2022-03-30T18:49:14Z")

</div>

I can take a look at the unresponsiveness issue after reattaching.

---

<div class="post-metadata">

**Author:** ![PolygonalSun](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/polygonalsun/32/2704_2.png) [@PolygonalSun](https://forum.babylonjs.com/u/PolygonalSun)\
**Post date:** [March 30, 2022, 10:41pm UTC](https://forum.babylonjs.com/t/issue-with-camera-attach-detach-in-5-0-0/28861/8 "2022-03-30T22:41:12Z")

</div>

Okay, so I’ve found the issue. The reason for the unresponsive click was due to a value for the active mouse button not being set back to a non-button value before the controls were detached. This [PR](https://github.com/BabylonJS/Babylon.js/pull/12302) should fix that issue.

---

<div class="post-metadata">

**Author:** ![satguru](https://avatars.discourse-cdn.com/v4/letter/s/e480ec/32.png) [@satguru](https://forum.babylonjs.com/u/satguru)\
**Post date:** [March 31, 2022, 7:22am UTC](https://forum.babylonjs.com/t/issue-with-camera-attach-detach-in-5-0-0/28861/9 "2022-03-31T07:22:59Z")

</div>

Yep that fixed the issue! 🙂  
Will test again when this is released.  
Thanks a bunch.
