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.
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;
}
};
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
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.