How to add sprint to FPS game?

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!

Hi @VisionsKlips

Can you show that where are you applying this power on the player?

I cant tell you the problem because I have zero context as to what is going on in your code.

what do you need to know to be able to help? i can give you it.

The entire script would be great.

1 Like

Alternatively, you can also add us to a dummy project.

1 Like

Here is the project where you can see the whole script. Click here to see my project.

Here is the project link. Click here to see my project.

Hey that link does not exist

Srry that was my old one before(i rage quitted)… Here is the new link: The New Link

Thanks, let me have a look

1 Like

Before you make a first person shooter, check this out:

ALUCARDS LETHAL MUNITIONS EXPO | Launch

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.

Yeah I read it. Btw, I already know how to fire bullets. Just the sprint mechanic needed for now. Imma add firing bullets after that.

Following is the code for how you can do it:

  • You will just need to add Shift under forward if
  • Multiply your power this any number
  • you can check logs to check speed
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);
    }
}

And this is the working link: PlayCanvas 3D HTML5 Game Engine

2 Likes

Thanks for the help.

1 Like

That link does not work either

i deleted it and forked @Faiq_Laiq 's version as it worked.

srry to bother u and not be active as much.