I’ve been trying to add sprint to my FPS prototype and I’m wondering why this sprinting script doesn’t change anything to the game. Here is the script:
if (app.keyboard.isPressed(pc.KEY_SHIFT) && (app.keyboard.isPressed(pc.KEY_W))) {
power = 15
} else {
power = 10
}
Power is just the speed of the player.Help would be appreciated!
I am actively working on it right now, so if you play the project and something breaks i probably just moved some stuff around and you need to refresh the page.
FirstPersonMovement.prototype.update = function (dt) {
// If a camera isn't assigned from the Editor, create one
if (!this.camera) {
this._createCamera();
}
var force = this.force;
var app = this.app;
// Get camera directions to determine movement directions
var forward = this.camera.forward;
var right = this.camera.right;
let power = this.power;
// movement
var x = 0;
var z = 0;
// Use W-A-S-D keys to move player
// Check for key presses
if (app.keyboard.isPressed(pc.KEY_A) || app.keyboard.isPressed(pc.KEY_Q)) {
x -= right.x;
z -= right.z;
}
if (app.keyboard.isPressed(pc.KEY_D)) {
x += right.x;
z += right.z;
}
if (app.keyboard.isPressed(pc.KEY_W)) {
x += forward.x;
z += forward.z;
power = this.power * (app.keyboard.isPressed(pc.KEY_SHIFT) ? 2 : 1);
console.log('power: ', power);
}
if (app.keyboard.isPressed(pc.KEY_S)) {
x -= forward.x;
z -= forward.z;
}
if (app.keyboard.isPressed(pc.KEY_SPACE) && this.canJump == 1) {
this.entity.rigidbody.applyImpulse(0, this.jumpImpulse, 0)
}
// use direction from keypresses to apply a force to the character
if (x !== 0 || z !== 0) {
force.set(x, 0, z).normalize().scale(power);
this.entity.rigidbody.applyForce(force);
}
}