Check if controls attached to camera?

Hi There,

I was trying to check if the controls are attached to the current camera, to be able to toggle controls on or off.

export const CanvasControlToggleBtn = function (scene:Scene) {
    var advancedTexture = AdvancedDynamicTexture.CreateFullscreenUI("UI");
    var button1 = Button.CreateSimpleButton("but1", "ON");
    button1.width = "150px"
    button1.height = "40px";
    button1.color = "white";
    button1.cornerRadius = 20;
    button1.background = "green";
    var attached = scene.attached;
    button1.onPointerUpObservable.add(function () {
        var camActive = scene.activeCamera
        if (attached) {
            console.log('detach');
            button1.textBlock.text = "ON";
            camActive.attachControl(canvas);
        }
        else {
            console.log('attach');
            button1.textBlock.text = "OFF";
            camActive.detachControl();

            // Error camera will be reattached after each mouseUP again
        }
    });
    advancedTexture.addControl(button1);
}

I’m using this snippet.
Which I got somewhere online, can’t remember, but the

    var attached = scene.attached;

Returns undefined.

What’s the correct way to check if the controls are attached to a specific camera?
Thanks!

@PolygonalSun might have an idea ?

but I think camera.inputs.attachedToElement should do the trick ?

Hey @Pieter_van_Stee, if you want to check which controls are attached to a given camera, you should be able to do it by using something like camera.inputs.attached. That will give you an array with all of the inputs currently attached

// Example: Get scene's active camera and store ref to attached inputs
var camera = scene.activeCamera;
var attachedInputs = camera.inputs.attached;

That will return an array of CameraInputs.

2 Likes

Thanks for the help here.
That did the trick…

    let attached:boolean = camActive?.inputs.attachedToElement;

This is what I needed “attachedToElement”.
Returns true/false