Camera - Keyboard issue

Hi,

I’m working in a simple FPS game with the Babylon 4 version that uses the Universal Camera, but I’m with a problem that I can’t find the solution (I spent some hours on It but no success).

When I enter the scene, I can’t use the keyboard until I press f12 (Eg: if I press f12 to show the console, then I press f12 again to hide, and then I can use the keyboard to move the camera). I’ve tested with Chrome and Firebox browsers.

I thought at first that the scene was loading before the keyboard listeners are prepared, but all my application is running inside the DOMContentLoaded event listener, then I think that this is not the problem.

If It can help, here is the Scene code: simple-3d-fps/FirstLevel.js at master · TiagoSilvaPereira/simple-3d-fps · GitHub


EDIT:

I discovered that if I load the First Level scene firstly, instead of the Menu scene, the issue does not occur. I think that the problem occurs because I am calling this code before loading a new scene:

engine.stopRenderLoop();

I noticed too that:

  • If I click out of the browser and then click in the browser again, It works.
  • If I add the Scene Inspector to debug the scene, It works well too
  • It occurs with 3.3 version too

Does someone know what is happening?

Thanks for helping!! :smiley:

This is probably because your canvas cannot be focused. Can you make sure that your canvas.zIndex is set to 1?

1 Like

Hi @Deltakosh thanks for responding :smiley: I tried to force z-index: 1 but the issue still occurs. The strange thing is that, if I load this scene firstly, the problem never occurs.

Maybe I’m making the scene transitions wrong?

At this moment I’m doing:

scene.dispose();
engine.stopRenderLoop();

// ... then I create the new scene and:
engine.runRenderLoop(() => {
    scene.render();
});

The complete code is here: simple-3d-fps/Level.js at master · TiagoSilvaPereira/simple-3d-fps · GitHub

I found this tutorial, but I’m concerned because It does not clear the scene with .dispose() before loading the next scene.

Thanks again!

I ran the game locally, and I believe I found it:

  1. When the initial scene is created, the canvas is not focused.
  2. The canvas is clicked and focused, which makes the scene’s input manager add key listeners to the canvas.
  1. You create a new scene, but because the canvas is already focused, the input manager of the new scene will not add the key listeners when the canvas is clicked.

Workaround:
this.scene._inputManager._onCanvasFocusObserver.callback();

1 Like

Thank you very much @Gijs I’ll try this solution ASAP

1 Like

I made a pull request that fixes it:

1 Like

That’s awesome @Gijs thank you very much!

1 Like