Jump Script Needs Improvement

Hello, here is my first person movement script which includes he jump script (triggered on when spacebar is pressed):

var FirstPersonMovement = pc.createScript('firstPersonMovement');

// optional, assign a camera entity, otherwise one is created
FirstPersonMovement.attributes.add('camera', {
    type: 'entity'
});

FirstPersonMovement.attributes.add('power', {
    type: 'number'
});

FirstPersonMovement.attributes.add('lookSpeed', {
    type: 'number'
});

// initialize code called once per entity
FirstPersonMovement.prototype.initialize = function() {
    this.force = new pc.Vec3();
    this.camera = null;
    this.eulers = new pc.Vec3();
    
    var app = this.app;
    
    this.grounded = true;

    // Listen for mouse move events
    app.mouse.on("mousemove", this._onMouseMove, this);

    // when the mouse is clicked hide the cursor
    app.mouse.on("mousedown", function () {
        app.mouse.enablePointerLock();
    }, this);

    // Check for required components
    if (!this.entity.collision) {
        console.error("First Person Movement script needs to have a 'collision' component");
    }

    if (!this.entity.rigidbody || this.entity.rigidbody.type !== pc.BODYTYPE_DYNAMIC) {
        console.error("First Person Movement script needs to have a DYNAMIC 'rigidbody' component");
    }
};

// update code called every frame
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;
    
    var pos = this.entity.getPosition();
    
    // 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;
    }

    if (app.keyboard.isPressed(pc.KEY_S)) {
        x -= forward.x;
        z -= forward.z;
    }
    if (app.keyboard.isPressed(pc.KEY_SPACE)) {
        if(this.grounded) {
            this.entity.rigidbody.applyImpulse(0, 200, 0);
            this.entity.sound.play("Boom");
            this.entity.sound.resume("Boom");
        }
    }
    
    if(pos.y > 1.2) {
        this.grounded = false;
    }
    else {
        this.grounded = true;
    }
    
    // use direction from keypresses to apply a force to the character
    if (x !== 0 && z !== 0) {
        force.set(x, 0, z).normalize().scale(this.power);
        this.entity.rigidbody.applyForce(force);
    }

    // update camera angle from mouse events
    this.camera.setLocalEulerAngles(this.eulers.y, this.eulers.x, 0);
};

FirstPersonMovement.prototype._onMouseMove = function (e) {
    // If pointer is disabled
    // If the left mouse button is down update the camera from mouse movement
    if (pc.Mouse.isPointerLocked() || e.buttons[0]) {
        this.eulers.x -= this.lookSpeed * e.dx;
        this.eulers.y -= this.lookSpeed * e.dy;
    }
};

FirstPersonMovement.prototype._createCamera = function () {
    // If user hasn't assigned a camera, create a new one
    this.camera = new pc.Entity();
    this.camera.setName("First Person Camera");
    this.camera.addComponent("camera");
    this.entity.addChild(this.camera);
    this.camera.translateLocal(0, 0.5, 0);
};

The jump works fine, however I want to make it so that the entity can only jump when the player is touching something, not just when the player’s position is under a set value. I need this because then the player can jump in the air. Is there any way to do this? Quick help would be appreciated, as this is due tomorrow.

all help is so much appreciated, thank you so much to anyone out there who can help. please do not be alarmed by the large script size, it is only a small part of the script that needs working on. :smiley:

thanks.

here is the project:
https://playcanvas.com/editor/scene/1321393

Hi @CRAYPHiSHQUESTIONS,

There are several topics on the forum on how to approach a jump functionality. Check some of these out below -

I looked at these but they still do not solve the problem. I need to make it so that the characters jump is limited to working only when the character is touching something. Thanks alot though.

In that case, you can use collisions and triggers to detect whether the character is touching something, and enable/disable the jump functionality accordingly.

I can set that up, I am just confused on what function I need to use in order to disable the jump script via another script. Is there any scripting reference on such function?

You can have a look at script communication to achieve this.

I think I have a solution, in this topic. it is returning an error however.

I have changed the script back to original for now, but what I was trying to do was make it so that if the entity was colliding with the rigidbody, it would set grounded to be true, else false. I have to go for a while but let me know if you think this is a solution that could work if done right. thanks for all the help, @DevilZ !

That method should work. Make sure to read through the guides for collisions, triggers and events, and you should be able to do it!

Thanks!

1 Like