[SOLVED] Jumping script ignoring grounded rule

It is probably a silly mistake, but my jumping script is not working the way it should. I have set a rule called “grounded” and if it is false, the player should not be able to jump. If it meets the if statements conditions, grounded is set to true and the player should be able to jump. I am trying to make it so that the if statement says: if the player is colliding or touching something, then the player can jump. else, false (they cannot) I do not know if I am using a wrong method, and if I should be using onTriggerEnter instead of onCollisionStart, but the player and objects it is colliding with both have rigid bodies and capsules.
Here is he script:

if (app.keyboard.isPressed(pc.KEY_SPACE)) {
        if(this.grounded === true) {
            this.entity.rigidbody.applyImpulse(0, 200, 0);
            this.entity.sound.play("Boom");
            this.entity.sound.resume("Boom");
        }
    }
    FirstPersonMovement.prototype.onCollisionStart = function (result) {
    if(result.other.rigidbody) {
        this.grounded = true;
    }
    else {
        this.grounded = false;
    }
    };

Could anyone let me know what I am doing wrong and how to fix it? any help is appreciated, and thanks. :smiley: .

You can’t set this.grounded to false in your onCollisionStart event. You need the onCollisionEnd event for that.

You can also do it differently. Then the logic would look something like below.

// update
    if (app.keyboard.wasPressed(pc.KEY_SPACE)) {
        if (this.grounded === true) {
            this.entity.rigidbody.applyImpulse(0, 1000, 0);
            this.entity.sound.play("Boom");
            this.entity.sound.resume("Boom");
            this.grounded = false;
        }
    }
// event
FirstPersonMovement.prototype.onCollisionStart = function (result) {
    if(result.other.rigidbody) {
        this.grounded = true;
    }
};

Hey, so I plugged the code in, in what I believed was the correct way:

if (app.keyboard.isPressed(pc.KEY_SPACE)) {
        if(this.grounded === true) {
            this.entity.rigidbody.applyImpulse(0, 200, 0);
            this.entity.sound.play("Boom");
            this.entity.sound.resume("Boom");
        }
    }
    
    // it is in the update
    if (app.keyboard.wasPressed(pc.KEY_SPACE)) {
        if (this.grounded === true) {
            this.entity.rigidbody.applyImpulse(0, 1000, 0);
            this.entity.sound.play("Boom");
            this.entity.sound.resume("Boom");
            this.grounded = false;
        }
    }

    FirstPersonMovement.prototype.onCollisionStart = function (result) {
    if(result.other.rigidbody) {
        this.grounded = true;
        }
    };

And it only allows me to jump one then never again. I am getting somewhere with this, but am I doing something wrong or is this the wrong way to approach this problem? I am probably doing something wrong, but let me know when you get the chance, thanks! :smiley: .

This is a separate function and does not belong inside the update function.

Oh okay, for some reason I didn’t see that. :sweat_smile: Thanks for helping me with this script, I really appreciate it now being possible to lose, haha. Thanks again, Albertos! :smiley: .