What's wrong with my jumping script?

So I have a jumping script that does work. However, I needed to make the jump feel better, so I tried to add something where when the player reaches the height of their jump, they fall faster than normal to make the jump feel less floaty. My code is not working though, and I know barely anything about JavaScript, so I didn’t think it would work. How would I make this work?
Here’s my code:

var force = 150; //how powerful the jump is
var fallMultiplier = 5; //How fast you fall after jumping
var y = this.GeolocationPosition;

Jump.prototype.initialize = function() {
    this.app.keyboard.on(pc.EVENT_KEYDOWN, this.onKeyDown, this);
};

Jump.prototype.update = function(dt) {
    if (this.app.keyboard.wasPressed(pc.KEY_SPACE)) {
        this.entity.rigidbody.applyImpulse(0, force, 0);
    }
};

if (y > 0.5) {
    this.app.systems.rigidbody.gravity * fallMultiplier();
}

Hi @OMN1B! I’m not sure about line 3 of your script, where do you define this.GeolocationPosition?
Also line 13 should be after line 17, because you close the update function with it.

I’m not entirely sure what GeolocationPosition even is, but I think it’s like a getPosition function

Then you can try to replace it with this.entity.getPosition().y.

it’s giving me this error:
TypeError: Cannot read properties of undefined (reading ‘getPosition’)

Yeah my bad. Can you try the code below?

var force = 150; //how powerful the jump is
var fallMultiplier = 5; //How fast you fall after jumping

Jump.prototype.initialize = function() {
    this.app.keyboard.on(pc.EVENT_KEYDOWN, this.onKeyDown, this);
};

Jump.prototype.update = function(dt) {
    if (this.app.keyboard.wasPressed(pc.KEY_SPACE)) {
        this.entity.rigidbody.applyImpulse(0, force, 0);
    }

    if (this.entity.getPosition().y > 0.5) {
        this.app.systems.rigidbody.gravity * fallMultiplier;
    }
};

It still doesn’t work, I might be going about implementing this the wrong way. It’s telling me that the fallMultiplier function doesn’t exist.

I see. You need to remove () on line 14. I have updated the code above.

@OMN1B This thread is a bit lengthy but covers a lot of questions about jumping.

Hope it helps

My code doesn’t work still, I also did a bit of testing, and it looks like the
“if (this.entity.getPosition().y > 0.5)” isn’t even executing. I can put my full project here too if that helps?
my project
it’s just a kind of testing ground to learn the basics

Hello @OMN1B,

Take a look at the script example below. I’m using rigidbody.applyForce(0, this.fallMultiplier * -1, 0) to apply force to your Player when he is not touching the ground. It isn’t too dissimilar to how you’re using this.entity.rigidbody.applyImpulse(0, force, 0) to make the Player jump.

See the full code below:

var Jump = pc.createScript('jump');

var force = 100; //how powerful the jump is
var fallMultiplier = 200; //How fast you fall after jumping

Jump.prototype.initialize = function () {
    this.app.keyboard.on(pc.EVENT_KEYDOWN, this.onKeyDown, this);
};

Jump.prototype.update = function (dt) {
    if (this.app.keyboard.wasPressed(pc.KEY_SPACE)) {
        if (this.entity.getPosition().y <= 0.6) {//checks to see if the player is on the ground before jumping
            this.entity.rigidbody.applyImpulse(0, force, 0);
        }
    }

    if (this.entity.getPosition().y > 0.7) {//checks to see if the y position is above a certain number before
        this.entity.rigidbody.applyForce(0, fallMultiplier * -1, 0); // New line here
};

I’ve tweaked a few values as well. If you want to take this further, I’d recommend you take a look at some of PlayCanvas’ scripting tutorials, as they can help you get a better understanding of how the code works.

2 Likes

Thank you, this was super helpful!

1 Like