I am trying to make an FPS game but the camera that I put on players can’t get past the screens borders. I saw the FPS movement post but the player movements and camera controls are two different scripts in my game.
var CameraControl = pc.createScript('cameraControl');
// Settings
CameraControl.prototype.initialize = function () {
this.pitch = 0;
this.yaw = 0;
this.sensitivity = 0.2;
this.centerX = window.innerWidth / 2;
this.centerY = window.innerHeight / 2;
this.lastMouseX = this.centerX;
this.lastMouseY = this.centerY;
// Starting Direction
var euler = this.entity.getLocalEulerAngles();
this.pitch = euler.x;
this.yaw = euler.y;
// Locking Mouse with a Click
this.app.mouse.on(pc.EVENT_MOUSEDOWN, this.onMouseDown, this);
// Unlocking Mouse by pressing the ESC button
this.app.keyboard.on(pc.EVENT_KEYDOWN, this.onKeyDown, this);
// Pointer Lock Status
this.pointerLocked = false;
// Follow the Mouse Movements
this.onMouseMoveBind = this.onMouseMove.bind(this);
};
// Ask for Point Lock when clicked with Mouse
CameraControl.prototype.onMouseDown = function (event) {
this.app.mouse.enablePointerLock();
document.addEventListener('mousemove', this.onMouseMoveBind);
document.body.style.cursor = 'none';
this.pointerLocked = true;
};
// Follow Mouse Mevement
CameraControl.prototype.onMouseMove = function (event) {
if (this.pointerLocked) {
// Follow Mouse Positions and change Directions
var movementX = event.clientX - this.lastMouseX;
var movementY = event.clientY - this.lastMouseY;
// Make Mouse still be able to go even when its hit a screen border
this.yaw -= movementX * this.sensitivity; // Yaw right/left
this.pitch -= movementY * this.sensitivity; // Pitch up/down
this.pitch = pc.math.clamp(this.pitch, -89, 89); // Pitch borders
// Save last Mouse positions
this.lastMouseX = event.clientX;
this.lastMouseY = event.clientY;
}
};
// Show Mouse again when pressed ESC button
CameraControl.prototype.onKeyDown = function (event) {
if (event.key === pc.KEY_ESCAPE) {
// Leave the Point Lock
document.exitPointerLock();
document.body.style.cursor = 'auto';
this.pointerLocked = false;
// Put Mouse at the center of the Screen or Leave it where it is
this.lastMouseX = this.centerX;
this.lastMouseY = this.centerY;
}
};
// Apply Rotation to Camera
CameraControl.prototype.update = function (dt) {
this.entity.setLocalEulerAngles(this.pitch, this.yaw, 0);
};
these are the codes thats in the Camera Control script.