How to solve "attachControl" undefined error

Hi everyone

I am trying to modify the camera for my tiled map, first I try to use camera.inputs.removeByType("FreeCameraMouseInput");
to remove the mouse actions to avoid to change the angle of the camera, but I want to active it when the camera position in y axis is less than 1000 :
if (camera.position.y < 1000) camera.inputs.attachInput(camera.inputs.attached.mouse);

But, I can change the camera angle when camera.position.y is less than 1000 and got the “attachControl” undefined error

for example there are a cube in below of the ground, and I want to move and rotate over the cube below the ground.

here is my PG :

and here is my code for this purpose :

  scene.registerBeforeRender(() => {
    camera.speed = Math.min(4500, Math.abs(camera.position.y * 0.05));
    if (camera.position.y > 0) {
    if (camera.position.y > 85000) camera.position.y = 85000;
     if (camera.position.y < 1000) camera.inputs.attachInput(camera.inputs.attached.mouse);

pinging @PolygonalSun

1 Like

Hey @Arash_Bagheri, lemme take a look and see what’s going on.

1 Like

Hey @PolygonalSun, Thank you so much, sorry for taking your precious time.

@Deltakosh Thank you so much :pray: :pray: :pray:

No worries, dude. So what I’ve found is that on Line 58, you’re trying to attach camera.inputs.attached.mouse after removing having initially removed it.

camera.attachControl(canvas, true);
if (camera.position.y < 1000) camera.inputs.attachInput(camera.inputs.attached.mouse);

When you use the removeByType function, it will delete the instance of the mouse input. This means that when you try to reattach it, it’s already been destroyed; therefore, you have nothing to attach.

You might want to use something like:
if (camera.position.y < 1000) camera.inputs.addMouse();
but you’ll want to make sure that you have some additional check in place (maybe a boolean flag?) so that you only run camera.inputs.addMouse() once when your position.y is less than 1000. This would be where I’d recommend to start.

1 Like

Yes, Thank you so much for your explain. its great. :pray: :pray:
sorry for taking your time.

I have replaced camera.inputs.addMouse() in my code but nothing happened and I can’t move the camera with the mouse :

Lemme take another look at what’s going on.

1 Like

So I changed a bit around but here’s what I’ve got: TiledMap (Fixed) | Babylon.js Playground

First, I removed the camera.inputs.removeByType("FreeCameraMouseInput"); from underneath the attachControl call and moved it into the registerBeforeRender() assignment. Next, I modified your check so that it will add the mouse back once and remove it when zoomed out:

 * Since your map changes to the box at 1200, I changed the check to 1200.
 * I also have a check to see if there is a mouse input attached already, if you're 
 * below 1200 and there's no mouse, add one. 
if (camera.position.y < 1200 && !camera.inputs.attached.mouse) {
    camera.inputs.add(new BABYLON.FreeCameraMouseInput());
 * This check will remove the mouse when we zoom out.  If we go above our 1200
 * boundary, check if there's a mouse.  If so we remove it, otherwise, do nothing.
 * Having both of these check is necessary to make sure that we don't remove the
 * mouse just after adding it.
else if (camera.position.y >= 1200 && camera.inputs.attached.mouse){
1 Like

Thank you so much @PolygonalSun .
thank you for sharing your knowledge with me and let me to learn from you. :pray:

1 Like