Locking camera to mouse (like in an FPS)

I’m looking to build a first-person experience, and one of the first things I noticed was the UniversalCamera (“the one to choose for first person shooter type games”) does not, in fact, track the mouse as one would expect (i.e. like Minecraft). The closest thing I found was this:

In that playground, the camera turns to face wherever you move towards, but still requires you to click and drag to turn the camera. I’m looking for functionality that locks my cursor to the center of the screen, such that my mouse movement directly controls the camera direction (without me needing to click).

My apologies if there is some simple fix I’m missing. Thanks in advance!

I used this PG for my project’s camera control via Vanilla JS:

Or check out how it is implemented via BabylonJS API:

1 Like

Don’t remember where I found this code but it’s simple and works

Mouse Pointer Lock example:

//Mouse Pointer Lock

var isLocked = false;  // Start without being locked

// On click event, request pointer lock
scene.onPointerDown = function (evt) {
    //true/false check if we're locked, faster than checking pointerlock on each single click.
    if (!isLocked) {
        canvas.requestPointerLock = canvas.requestPointerLock || canvas.msRequestPointerLock || canvas.mozRequestPointerLock || canvas.webkitRequestPointerLock;
        if (canvas.requestPointerLock) {
            canvas.requestPointerLock();
        }
    }
};

// Event listener when the pointerlock is updated (or removed by pressing ESC for example).
var pointerlockchange = function () {
    var controlEnabled = document.mozPointerLockElement || document.webkitPointerLockElement || document.msPointerLockElement || document.pointerLockElement || null;
    // If the user is already locked
    if (!controlEnabled) {
        //camera.detachControl(canvas);
        isLocked = false;
    } else {
        //camera.attachControl(canvas);
        isLocked = true;
    }
};

// Attach events to the document
document.addEventListener("pointerlockchange", pointerlockchange, false);
document.addEventListener("mspointerlockchange", pointerlockchange, false);
document.addEventListener("mozpointerlockchange", pointerlockchange, false);
document.addEventListener("webkitpointerlockchange", pointerlockchange, false);

(btw new Havok player controls are supposed to be released soon in upcoming update, very much looking forward to that)

1 Like

Hi and welcome to the Community and then, thx for sharing.
Indeed, this code would work to request a pointer lock. I’m currently using it as a base in my scene with multiple and ‘hybrid-mode’ cameras (where you can switch between freelook and pointer mode). I would just add to the above that pointer lock is managed by the browser and follows browser policies. There are condition to a successful request of a pointerlock. I.E. in chrome, pointerlock can only be requested on a user gesture and else, pointerlock cannot be called without a delay in switching mode (of about 1s in chrome that is, from my knowledge, the most restrictive browser in this aspect).